这个工具类亲测实用,兼容安卓8.0,废话不说上代码:
package com.djfb.utils;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.v4.content.FileProvider;
import java.io.File;
import java.io.IOException;
/**
* Created by HJS on 2018/4/8.
*/
public class DownloadUtils {
//下载器
private static DownloadManagerdownloadManager;
//下载的ID
private static long downloadId;
private static StringapkFileName ="djfb.apk";
private static Context mcontext;
//广播监听下载的各个状态
private static BroadcastReceiverreceiver =new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
checkStatus();
}
};
//下载apk
public static void downloadAPK(String url) {
//删除旧包
File oldApkFile =new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), apkFileName);
if(oldApkFile.exists()){
oldApkFile.delete();
}
//创建下载任务
DownloadManager.Request request =new DownloadManager.Request(Uri.parse(url));
//移动网络情况下是否允许漫游
request.setAllowedOverRoaming(false);
//在通知栏中显示,默认就是显示的
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
request.setTitle("电竞风暴");
request.setDescription("深圳电竞风暴科技有限公司");
request.setVisibleInDownloadsUi(true);
request.setMimeType("application/vnd.android.package-archive");
//设置下载的路径
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS , apkFileName);
//获取DownloadManager
downloadManager = (DownloadManager) mcontext.getSystemService(Context.DOWNLOAD_SERVICE);
//将下载请求加入下载队列,加入下载队列后会给该任务返回一个long型的id,通过该id可以取消任务,重启任务、获取下载的文件等等
downloadId =downloadManager.enqueue(request);
//动态注册广播接收者,监听下载状态
mcontext.registerReceiver(receiver,
new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
//检查下载状态
private static void checkStatus() {
DownloadManager.Query query =new DownloadManager.Query();
//通过下载的id查找
query.setFilterById(downloadId);
Cursor c =downloadManager.query(query);
if (c.moveToFirst()) {
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
switch (status) {
//下载暂停
case DownloadManager.STATUS_PAUSED:
break;
//下载延迟
case DownloadManager.STATUS_PENDING:
break;
//正在下载
case DownloadManager.STATUS_RUNNING:
break;
//下载完成
case DownloadManager.STATUS_SUCCESSFUL:
//下载完成安装APK
installAPK();
break;
//下载失败
case DownloadManager.STATUS_FAILED:
ToastUtils.showToast("下载失败");
break;
}
}
c.close();
}
/**
* 兼容 android 8.0
*/
private static void installAPK() {
File apkFile =
new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), apkFileName);
Intent intent =new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
//如果没有设置SDCard写权限,或者没有sdcard,apk文件保存在内存中,需要授予权限才能安装
String[] command = {"chmod", "777", apkFile.toString()};
ProcessBuilder builder =new ProcessBuilder(command);
builder.start();
if (Build.VERSION.SDK_INT >=24) {
Uri apkUri = FileProvider.getUriForFile(context, context.getPackageName() +".updateFileProvider", apkFile);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
}else {
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
}
startActivity(intent);
}catch (IOException ignored) {
ignored.printStackTrace();
}
}
}
有问题请留言,喜欢点波赞吧