apk版本更新

一. apk下载

public void downloadTask(String url){
        File fileApk = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/"+MyConfig.DOWNAPK);
        if (fileApk.exists()){
            fileApk.delete();
        }
        DownloadManager downloadManager = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.setAllowedOverRoaming(false);
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
        request.setTitle("车道手持机");
        request.setMimeType("application/vnd.android.package-archive");
        request.setDescription("文件正在下载中......");
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,MyConfig.DOWNAPK);
        long id = downloadManager.enqueue(request);
        ProgressDialog dialog = new ProgressDialog(SplashActivity.this);
        dialog.setTitle("版本更新");
//        dialog.setProgressPercentFormat(NumberFormat.getCurrencyInstance().);
        dialog.setIndeterminate(false);
        dialog.setMessage("文件正在下载中");
        dialog.setCancelable(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.show();
        Timer timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                DownloadManager.Query query = new DownloadManager.Query();
                Cursor cursor = downloadManager.query(query.setFilterById(id));

                if (cursor != null && cursor.moveToFirst()){
                    int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
                    switch (status){
                        case DownloadManager.STATUS_RUNNING://正在下载
                            DubugLog.logd("update","正在下载");
                            int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                            int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                            int pro =  (bytes_downloaded * 100) / bytes_total;
                            dialog.setProgress(pro);
//                            dialog.cancel();
//                            installApk( new File(Environment.DIRECTORY_DOWNLOADS+"/"+MyConfig.DOWNAPK));
                            break;
                        case DownloadManager.STATUS_FAILED://下载失败
                            DubugLog.logd("update","下载失败");
                            dialog.cancel();
                            timer.cancel();
                            task.cancel();
                            DialogUtil.showDialog1(SplashActivity.this, "新版本更新失败,请检查网络", new DialogUtil.DialogCallback() {
                                @Override
                                public void dialogSuccess() {
                                    finish();
                                }
                            });
                            break;
                        case DownloadManager.STATUS_SUCCESSFUL://下载成功
                            DubugLog.logd("update","下载成功");
                            dialog.cancel();
                            timer.cancel();
                            task.cancel();
                            installApk( new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) +"/"+ MyConfig.DOWNAPK));
                            break;

                    }

                }
                cursor.close();
            }
        };
        task.run();
        timer.schedule(task,0,1000);




    }

二. apk安装

    public void installApk(File apkFile) {
        Intent installApkIntent = new Intent();
        installApkIntent.setAction(Intent.ACTION_VIEW);
        installApkIntent.addCategory(Intent.CATEGORY_DEFAULT);
        installApkIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            installApkIntent.setDataAndType(FileProvider.getUriForFile(this, SplashActivity.this.getPackageName()+".fileprovider", apkFile), "application/vnd.android.package-archive");
            installApkIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            installApkIntent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
        }
        if (getPackageManager().queryIntentActivities(installApkIntent, 0).size() > 0) {
            startActivity(installApkIntent);
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值