android 下载安装包更新app
2024-10-21 02:48:53
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"/>
最新文章
- [转载]跨域iframe高度自适应
- web兼容学习分析笔记--块级、内联、内联块级元素
- 【干货】JS版汉字与拼音互转终极方案,附简单的JS拼音输入法
- spring入门(四)【面向切面编程】
- HTML总结笔记
- Spring bean的作用域和生命周期
- 编写高性能SQL
- Ubuntu 更新源
- 最小生成树--Prim算法,基于优先队列的Prim算法,Kruskal算法,Boruvka算法,“等价类”UnionFind
- js种的循环语句
- 关于maven-jetty-plugin 自动重启问题
- thinkphp调试技巧
- 初识Haskell 三:函数function
- 从0开始的Python学习001快速上手手册
- IntelliJ IDEA编辑器光标定位错误的问题!
- Python——Flask框架——数据库
- Newtonsoft.Json WindowPhone7.1
- NLP 第10章 基于深度学习的NLP 算法
- python入门 -- 学习笔记2
- w7 目录