android 下载安装包更新app
阅读原文时间:2023年07月10日阅读:1

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 增加配置

  <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


今天又测试了一波 bug一堆哦

首先是http 我哪有https呢,我一个客户端技术只能搞个ftp模拟下载 安卓开启http

在 xml/network_security_config 在android res 目录中加 目录xml 内容如下


Manifest 中配置

网络安装引用的权限