public class DownLoadApk { public Context context; public DownLoadApk(Context context) { this.context = context; } /* * 从服务器中下载APK */ public void downLoadApk(final VersionInfo.Version versionInfo) { final ProgressDialog pd; //进度条对话框 pd = new ProgressDialog(context); pd.setCancelable(false);// 设置点击屏幕Dialog不消失 pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setMessage("正在下载更新"); pd.show(); new Thread() { @Override public void run() { try { File file = getFileFromServer(versionInfo.getVersionUrl(), pd); //安装APk installApk(file, context); pd.dismiss(); //结束掉进度条对话框 } catch (Exception e) { e.printStackTrace(); } } }.start(); } /** * 下载方法 * * @param path * @param pd * @return * @throws Exception */ public File getFileFromServer(String path, ProgressDialog pd) throws Exception { // 如果相等的话表示当前的sdcard挂载在手机上并且是可用的 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); // 获取到文件的大小 pd.setMax(conn.getContentLength()); InputStream is = conn.getInputStream(); File file = new File(Environment.getExternalStorageDirectory(), "gc.apk"); FileOutputStream fos = new FileOutputStream(file); BufferedInputStream bis = new BufferedInputStream(is); byte[] buffer = new byte[1024]; int len; int total = 0; while ((len = bis.read(buffer)) != -1) { fos.write(buffer, 0, len); total += len; // 获取当前下载量 pd.setProgress(total); } fos.close(); bis.close(); is.close(); return file; } else { return null; } } /** * 安装Apk * * @param file * @param context */ public void installApk(File file, Context context) { Intent installIntent = new Intent(Intent.ACTION_VIEW); installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); //判读版本是否在7.0以上 if (Build.VERSION.SDK_INT >= 24) { //参数1 上下文, 参数2 Provider主机地址 和清单文件中保持一致 参数3 共享的文件 Uri apkUri = FileProvider.getUriForFile(context, "com.example.asus.customer.provider", file); //添加这一句表示对目标应用临时授权该Uri所代表的文件 installIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); installIntent.setDataAndType(apkUri, "application/vnd.android.package-archive"); //兼容8.0( 8.0 后需要加 未知来源的手动获取权限 来调起安装页面) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { boolean hasInstallPermission = context.getPackageManager().canRequestPackageInstalls(); if (!hasInstallPermission) { startInstallPermissionSettingActivity(context); return; } } } else { installIntent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); } context.startActivity(installIntent); // //判断是否在Android7.0以上 // Intent intent = new Intent(); // intent.setAction(Intent.ACTION_VIEW); // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // if (Build.VERSION.SDK_INT >= 24) { // //参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致 参数3 共享的文件 // Uri apkUri = FileProvider.getUriForFile(context, "com.example.asus.customer.provider", file); // //添加这一句表示对目标应用临时授权该Uri所代表的文件 // intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); // } else { // intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); // } // context.startActivity(intent); } /** * 跳转到设置-允许安装未知来源-页面 */ @RequiresApi(api = Build.VERSION_CODES.O) private void startInstallPermissionSettingActivity(final Context context) { //注意这个是8.0新API Uri packageURI = Uri.parse("package:" + context.getPackageName()); Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } }
更新Apk工具类
最新推荐文章于 2021-05-27 22:56:30 发布