1下载
public void download(String url) { Utils.Loge("download",url); Uri uri = Uri.parse(url); DownloadManager.Request request = new DownloadManager.Request(uri); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); String appName = getAppName(activity); request.setDestinationInExternalFilesDir(activity, Environment.DIRECTORY_DOWNLOADS, appName + ".apk"); DownloadManager downManager = (DownloadManager)activity.getSystemService(Context.DOWNLOAD_SERVICE); long Id = downManager.enqueue(request); listener(Id,appName + ".apk"); }
2监听下载
private void listener(final long Id,String filePath) { String mypath = Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"Android"+File.separator+"data"+ File.separator+ activity.getPackageName()+File.separator + "files" + File.separator + Environment.DIRECTORY_DOWNLOADS + File.separator + filePath; //mypath = Environment.DIRECTORY_DOWNLOADS + File.separator + filePath; //File fileFull = new File(path, filePath); IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE); cReceiver = new Complete(); // 这是个广播器在下文中有完整代码 cReceiver.setId(Id); cReceiver.setContext(activity); cReceiver.setRunnable(new Runnable() { @Override public void run() { File file= new File(mypath); Intent intent = new Intent(Intent.ACTION_VIEW); Uri data; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { data = FileProvider.getUriForFile(activity, "com.haiwaiisland.adventure.fileprovider", file); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); } else { data = Uri.fromFile(file); } intent.setDataAndType(data, "application/vnd.android.package-archive"); activity.startActivity(intent); } }); activity.registerReceiver(cReceiver, intentFilter); }
//com.haiwaiisland.adventure.fileprovider 这个要在Manifest中配置
package com.BoomGames.Game; import android.app.Activity; import android.app.DownloadManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class Complete extends BroadcastReceiver { private long mId; private Activity mContext; private Runnable mRunnable; public void setId(long id){ mId = id; } public void setContext(Activity context){ mContext = context; } public void setRunnable(Runnable run){ mRunnable = run; } @Override public void onReceive(Context context, Intent intent) { long ID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); if (ID == mId) { if(mRunnable != null){ mRunnable.run(); } else{ } } } }
3Manifest 增加配置
<provider android:name="androidx.core.content.FileProvider" android:authorities="com.haiwaiisland.adventure.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"/> </provider>
//androidx.core.content.FileProvider 这个是androidx 以前的版本是 android.support.v4.content.FileProvider
//com.haiwaiisland.adventure.fileprovider 这个是 包名.fileprovider
//xml/file_paths 在android res 目录中加 目录xml 在这个目录中新建 file_paths.xml 文件
4 file_paths.xml
<?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="http://schemas.android.com/apk/res/android"> <paths> <external-path path="" name="download"/> </paths> </resources>
今天又测试了一波 bug一堆哦
首先是http 我哪有https呢,我一个客户端技术只能搞个ftp模拟下载 安卓开启http
在 xml/network_security_config 在android res 目录中加 目录xml 内容如下
<?xml version="1.0" encoding="utf-8"?> <network-security-config> <base-config cleartextTrafficPermitted="true" /> </network-security-config>
Manifest 中配置
<application android:networkSecurityConfig="@xml/network_security_config">
网络安装引用的权限
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· dotnet 9 通过 AppHostRelativeDotNet 指定自定义的运行时路径
· 如何统计不同电话号码的个数?—位图法
· C#高性能开发之类型系统:从 C# 7.0 到 C# 14 的类型系统演进全景
· 从零实现富文本编辑器#3-基于Delta的线性数据结构模型
· 记一次 .NET某旅行社酒店管理系统 卡死分析
· 用c#从头写一个AI agent,实现企业内部自然语言数据统计分析
· 三维装箱问题(3D Bin Packing Problem, 3D-BPP)
· Windows上,10分钟构建一个本地知识库
· dotnet 9 通过 AppHostRelativeDotNet 指定自定义的运行时路径
· 使用 AOT 编译保护 .NET 核心逻辑,同时支持第三方扩展