Android 新版本的出现 ,适配Android8.0未知来源应用安装权限处理
1、在清单文件中增加请求安装权限
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
private void showChangCityDialog() {
CustomDialog.Builder builder = new CustomDialog.Builder(mContext);
builder.setTitleStr("提示");
builder.setBodysStr("安装应用需要打开未知来源权限,请去设置中开启权限");
builder.setLeftbtnStr("取消");
builder.setRightbtnStr("设置");
builder.setLeftBtnColor(R.color.color_333333);
builder.setrRightBtnColor(R.color.C6);
builder.setOnClickListener(new CustomDialog.DialogOnItemClickListener() {
@Override
public void leftClick() {
finish();
}
@Override
public void rightClick() {
//兼容8.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
boolean hasInstallPermission = mContext.getPackageManager().canRequestPackageInstalls();
if (!hasInstallPermission) {
startInstallPermissionSettingActivity();
} else {
//再次执行安装流程,包含权限判等
if (!TextUtils.isEmpty(appUrl)) {
//再次启动安装流程
installApk(appUrl);
}
}
}
}
});
builder.create().show();
}
/**
* 跳转到设置-允许安装未知来源-页面
*/
@RequiresApi(api = Build.VERSION_CODES.O)
private void startInstallPermissionSettingActivity() {
Uri packageURI = Uri.parse("package:" + BuildConfig.APPLICATION_ID);
//注意这个是8.0新API
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);
startActivityForResult(intent, 1);
}
image
网友评论