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);
        }
    }

 

發佈了130 篇原創文章 · 獲贊 6 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章