這個(gè)工具類親測(cè)實(shí)用,兼容安卓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;
? //廣播監(jiān)聽下載的各個(gè)狀態(tài)
? 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();
? ? }
//創(chuàng)建下載任務(wù)
? ? DownloadManager.Request request =new DownloadManager.Request(Uri.parse(url));
? ? //移動(dòng)網(wǎng)絡(luò)情況下是否允許漫游
? ? request.setAllowedOverRoaming(false);
? ? //在通知欄中顯示,默認(rèn)就是顯示的
? ? request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
? ? request.setTitle("電競(jìng)風(fēng)暴");
? ? request.setDescription("深圳電競(jìng)風(fēng)暴科技有限公司");
? ? request.setVisibleInDownloadsUi(true);
? ? request.setMimeType("application/vnd.android.package-archive");
? ? //設(shè)置下載的路徑
? ? request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS , apkFileName);
? ? //獲取DownloadManager
? ? downloadManager = (DownloadManager) mcontext.getSystemService(Context.DOWNLOAD_SERVICE);
? ? //將下載請(qǐng)求加入下載隊(duì)列,加入下載隊(duì)列后會(huì)給該任務(wù)返回一個(gè)long型的id,通過該id可以取消任務(wù),重啟任務(wù)、獲取下載的文件等等
? ? downloadId =downloadManager.enqueue(request);
? ? //動(dòng)態(tài)注冊(cè)廣播接收者,監(jiān)聽下載狀態(tài)
? ? mcontext.registerReceiver(receiver,
? ? ? new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
? }
//檢查下載狀態(tài)
? 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 {
//如果沒有設(shè)置SDCard寫權(quán)限,或者沒有sdcard,apk文件保存在內(nèi)存中,需要授予權(quán)限才能安裝
? ? ? 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();
? ? }
}
}
有問題請(qǐng)留言,喜歡點(diǎn)波贊吧