Android 新版本的出現(xiàn) 厌丑,適配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 {
//再次執(zhí)行安裝流程,包含權限判等
if (!TextUtils.isEmpty(appUrl)) {
//再次啟動安裝流程
installApk(appUrl);
}
}
}
}
});
builder.create().show();
}
/**
* 跳轉(zhuǎn)到設置-允許安裝未知來源-頁面
*/
@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);
}