检查app版本更新并调用系统下载并显示到通知栏

1.下载apk代码片段

private void downloadApk(String url, Activity activity) {

    DownloadManager downloadManager=(DownloadManager)activity.getSystemService(Context.DOWNLOAD_SERVICE);
        // 开始下载
        Uri resource = Uri.parse(url);
        DownloadManager.Request request = new DownloadManager.Request(resource);
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
        request.setAllowedOverRoaming(false);
        // 设置文件类型
        MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
        String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url));
        request.setMimeType(mimeString);
        // 在通知栏中显示
      request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
        request.setVisibleInDownloadsUi(true);
        // sdcard的目录下的download文件夹
        request.setDestinationInExternalPublicDir("/download/",
                url.substring(url.lastIndexOf("/") + 1, url.length()));

        request.setTitle(getApplication().getApplicationInfo().loadLabel(getPackageManager()));
    //将下载apk的id传给application
        long id = downloadManager.enqueue(request);
        MyApplication application = (MyApplication) getApplication();
        application.downLoadId = id;
 }

2.在application中onCreate注册广播

IntentFilter downLoadfilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {

   if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
                VersionUtils.installApk(getDownloadedFileName(), context);
                unregisterReceiver(downloadReceiver);
            }
        }
    };
    registerReceiver(downloadReceiver, downLoadfilter);文件名代码片段

//获取下载apk的名字
//针对安卓7.0,DownloadManager.COLUMN_LOCAL_FILENAME已不适用
private String getDownloadedFileName() {

        String fileName = "";

        DownloadManager downloadManager = (DownloadManager) getSystemService(Activity.DOWNLOAD_SERVICE);
        DownloadManager.Query query = new DownloadManager.Query();
        query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL);

        Cursor c = downloadManager.query(query);
        while (c.moveToNext()) {
            long id = c.getLong(c.getColumnIndex(DownloadManager.COLUMN_ID));
            if (id == downLoadId) {
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
                    String fileUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                    fileName = Uri.parse(fileUri).getPath();
                } else {
                    fileName = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
                }
                break;
            }
        }
        if (c!=null){
            c.close();
        }
        return fileName;
    }

4.安装apk代码片段

public static void installApk(String path, Context act) {
        File apkfile = new File(path);
        if (!apkfile.exists()) {
            return;
        }
        // 通过Intent安装APK文件
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive");
        act.startActivity(intent);
    }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值