package com.demo.lib.common.upgrade; import android.annotation.SuppressLint; import android.app.DownloadManager; import android.app.DownloadManager.Request; import android.app.IntentService; 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 androidx.core.content.FileProvider; import per.wsj.library.BuildConfig; import android.util.Log; import android.webkit.MimeTypeMap; import java.io.File; @SuppressLint("NewApi") public class UpdateService extends IntentService { private long mDownLoadId; public UpdateService() { super("UpdateService"); } @Override protected void onHandleIntent(Intent intent) { DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); DownloadManager.Request request = new DownloadManager.Request(Uri.parse(intent.getStringExtra("download_url"))); request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI); request.setAllowedOverRoaming(false); //设置文件类型 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(intent.getStringExtra("download_url"))); request.setMimeType(mimeString); //在通知栏中显示 request.setNotificationVisibility(Request.VISIBILITY_VISIBLE); request.setVisibleInDownloadsUi(true); // sdcard的目录下的download文件夹 request.setDestinationInExternalPublicDir("/download/", intent.getStringExtra("file_name")); request.setTitle(intent.getStringExtra("notification_title")); request.setDescription(intent.getStringExtra("notification_description")); mDownLoadId = downloadManager.enqueue(request); registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); } private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // 这里可以取得下载的id,这样就可以知道哪个文件下载完成了。适用与多个下载任务的监听 if (intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0) == mDownLoadId) { queryDownloadStatus(context, intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0)); } } }; private void queryDownloadStatus(Context context, long downloadId) { DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); DownloadManager.Query query = new DownloadManager.Query(); 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: Log.v("DownloadManager", "STATUS_PAUSED"); case DownloadManager.STATUS_PENDING: Log.v("DownloadManager", "STATUS_PENDING"); case DownloadManager.STATUS_RUNNING: // 正在下载,不做任何事情 Log.v("DownloadManager", "STATUS_RUNNING"); break; case DownloadManager.STATUS_SUCCESSFUL: Intent intent = new Intent(Intent.ACTION_VIEW); File file = new File("file://" + c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))); //判断是否是AndroidN以及更高的版本 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri contentUri = FileProvider.getUriForFile(context.getApplicationContext(), BuildConfig.APPLICATION_ID + ".fileprovider", file); intent.setDataAndType(contentUri, "application/vnd.android.package-archive"); } else { Uri uri = Uri.fromFile(file); intent.setDataAndType(uri, "application/vnd.android.package-archive"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } context.startActivity(intent); // 完成 Log.v("DownloadManager", "STATUS_SUCCESSFUL"); break; case DownloadManager.STATUS_FAILED: // 清除已下载的内容,重新下载 Log.v("DownloadManager", "STATUS_FAILED"); downloadManager.remove(downloadId); break; } } } }