DownloadUtils.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.bgy.autosale.payutil;
  2. import android.app.Activity;
  3. import android.app.DownloadManager;
  4. import android.content.BroadcastReceiver;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.IntentFilter;
  8. import android.database.Cursor;
  9. import android.net.Uri;
  10. import android.os.Build;
  11. import android.util.Log;
  12. import android.widget.Toast;
  13. /**
  14. * 用于在原生上安装的应用
  15. */
  16. public class DownloadUtils {
  17. //下载器
  18. private DownloadManager downloadManager;
  19. //上下文
  20. private Activity mContext;
  21. //下载的ID
  22. private long downloadId;
  23. public DownloadUtils(Activity context) {
  24. this.mContext = context;
  25. }
  26. //下载apk
  27. public void downloadAPK(String url, String name) {
  28. //创建下载任务
  29. DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
  30. //移动网络情况下是否允许漫游
  31. request.setAllowedOverRoaming(false);
  32. //在通知栏中显示,默认就是显示的
  33. request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
  34. request.setTitle("新版本apk");
  35. request.setDescription("经理平板apk下载");
  36. request.setVisibleInDownloadsUi(true);
  37. //设置下载的路径
  38. // request.setDestinationInExternalPublicDir(Environment.getExternalStorageDirectory().getAbsolutePath() , name);
  39. //获取DownloadManager
  40. downloadManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
  41. //将下载请求加入下载队列,加入下载队列后会给该任务返回一个long型的id,通过该id可以取消任务,重启任务、获取下载的文件等等
  42. downloadId = downloadManager.enqueue(request);
  43. //注册广播接收者,监听下载状态
  44. mContext.registerReceiver(receiver,
  45. new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
  46. }
  47. //广播监听下载的各个状态
  48. private BroadcastReceiver receiver = new BroadcastReceiver() {
  49. @Override
  50. public void onReceive(Context context, Intent intent) {
  51. checkStatus();
  52. }
  53. };
  54. //检查下载状态
  55. private void checkStatus() {
  56. DownloadManager.Query query = new DownloadManager.Query();
  57. //通过下载的id查找
  58. query.setFilterById(downloadId);
  59. Cursor c = downloadManager.query(query);
  60. if (c.moveToFirst()) {
  61. int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
  62. Log.d("TTT", "检查下载进度 " + status);
  63. switch (status) {
  64. //下载暂停
  65. case DownloadManager.STATUS_PAUSED:
  66. break;
  67. //下载延迟
  68. case DownloadManager.STATUS_PENDING:
  69. break;
  70. //正在下载
  71. case DownloadManager.STATUS_RUNNING:
  72. break;
  73. //下载完成
  74. case DownloadManager.STATUS_SUCCESSFUL:
  75. //下载完成安装APK
  76. installAPK();
  77. break;
  78. //下载失败
  79. case DownloadManager.STATUS_FAILED:
  80. Toast.makeText(mContext, "下载失败", Toast.LENGTH_SHORT).show();
  81. break;
  82. }
  83. }
  84. c.close();
  85. }
  86. //下载到本地后执行安装
  87. private void installAPK() {
  88. //获取下载文件的Uri
  89. Uri downloadFileUri = downloadManager.getUriForDownloadedFile(downloadId);
  90. Log.d("TTT", "installAPK " + downloadFileUri);
  91. if (downloadFileUri != null) {
  92. boolean allow = true;
  93. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  94. allow = mContext.getPackageManager().canRequestPackageInstalls();
  95. }
  96. Log.d("TTTT", "没有权限更新应用");
  97. Intent intent = new Intent(Intent.ACTION_VIEW);
  98. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  99. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  100. //添加这一句表示对目标应用临时授权该Uri所代表的文件
  101. intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  102. }
  103. intent.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");
  104. mContext.startActivity(intent);
  105. mContext.unregisterReceiver(receiver);
  106. }
  107. }
  108. }