Android 11
APK下载位置兼容
Android5.0,Android5.1不支持断点续传且apk文件必须放到外置存储卡(代码安装apk出现提示错误:解析包时出现错误),不能放在内部存储卡(Android7.0支持apk存放在内部存储卡)。
public void installApk(String path){
File apkfile = new File(path);
if (!apkfile.exists()) {
return;
}
Intent intent = new Intent(Intent.ACTION_VIEW);
//兼容7.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 加这个,会导致升级后有多个任务栈的问题,先在startupactivity中处理了,后续再想办法
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".fileprovider", apkfile);
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
//兼容8.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
boolean hasInstallPermission = mContext.getPackageManager().canRequestPackageInstalls();
if (!hasInstallPermission) {
YKToast.showToast(mContext, mContext.getString(R.string.str_install_unknow_apk_note), Toast.LENGTH_SHORT);
startInstallPermissionSettingActivity();
return;
}
}
} else {
String filePath = "file://" + apkfile.toString();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.parse(filePath),"application/vnd.android.package-archive");
}
mContext.startActivityForResult(intent, RequestCodes.INSTALL_APK_CODE);
}