直接代码复制吧 从下载apk开始
private void downloadAPK() { // // 判断sd卡状态是否正常 // if (!Environment.getExternalStorageState().equals( // Environment.MEDIA_MOUNTED)) { // Toast.makeText(getApplicationContext(), "sd卡异常", Toast.LENGTH_SHORT).show(); // return; // } final ProgressDialog pDialog = new ProgressDialog(this); // 设置进度条为横向进度条 pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // pDialog.show(); pDialog.setCanceledOnTouchOutside(false); //点击progress以外的地方不消失 OkHttpUtils .get() .url(Global.UPGRADE_NEW) .build() .execute(new FileCallBack(Environment.getExternalStorageDirectory().getAbsolutePath(), "universe3000.apk") { @Override public void onError(Call call, Exception e, int id) { // 下载失败后进度条消失 pDialog.dismiss(); } @Override public void onResponse(File response, int id) { installAPK(response); } @Override public void inProgress(float progress, long total, int id) { super.inProgress(progress, total, id); pDialog.setMax(100); pDialog.setProgress((int) (progress * 100)); } @Override public void onBefore(okhttp3.Request request, int id) { super.onBefore(request, id); pDialog.show(); } }); } /* * 安装apk * * @param apkFile */ private static final int REQUEST_CODE_INSTALL = 1001; protected void installAPK(File apkFile) { // 隐式调用系统的安装页面 // <intent-filter> // <action android:name="android.intent.action.VIEW" /> // <category android:name="android.intent.category.DEFAULT" /> // <data android:scheme="content" /> // <data android:scheme="file" /> // <data android:mimeType="application/vnd.android.package-archive" /> // </intent-filter>
Intent intent = new Intent(); if(Build.VERSION.SDK_INT>=24) { //判读版本是否在7.0以上 //添加这一句表示对目标应用临时授权该Uri所代表的文件 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); }else { // intent.setAction("android.intent.action.VIEW"); intent.setAction(Intent.ACTION_VIEW); intent.addCategory("android.intent.category.DEFAULT"); } intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivityForResult(intent, REQUEST_CODE_INSTALL); }