Android 手機跳轉到許可權管理介面彙總
阿新 • • 發佈:2019-01-05
概述
最近專案中遇到這樣個需求場景:
當我們的使用者使用App時不小心拒絕了某項必要許可權,而導致無法正常使用。這時候希望重新去開啟該許可權,那麼問題來了,Android廠家定製的room五花八門,很多時候卻發現找不到許可權管理的入口。為了解決這一問題,如果我們應用中直接提供許可權管理入口給使用者,是不是會很方便的解決使用者這一困擾呢?經過一番研究,整理出了大部分國產手機直接開啟許可權管理介面的方法:
華為
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("packageName" , BuildConfig.APPLICATION_ID);
ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity");
intent.setComponent(comp);
startActivity(intent);
魅族
Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent .putExtra("packageName", BuildConfig.APPLICATION_ID);
startActivity(intent);
小米
Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
ComponentName componentName = new ComponentName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
intent.setComponent(componentName);
intent.putExtra("extra_pkgname", BuildConfig.APPLICATION_ID);
startActivity(intent);
索尼
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("packageName", BuildConfig.APPLICATION_ID);
ComponentName comp = new ComponentName("com.sonymobile.cta", "com.sonymobile.cta.SomcCTAMainActivity");
intent.setComponent(comp);
startActivity(intent);
OPPO
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("packageName", BuildConfig.APPLICATION_ID);
ComponentName comp = new ComponentName("com.color.safecenter", "com.color.safecenter.permission.PermissionManagerActivity");
intent.setComponent(comp);
startActivity(intent);
LG
Intent intent = new Intent("android.intent.action.MAIN");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("packageName", BuildConfig.APPLICATION_ID);
ComponentName comp = new ComponentName("com.android.settings", "com.android.settings.Settings$AccessLockSummaryActivity");
intent.setComponent(comp);
startActivity(intent);
樂視
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("packageName", BuildConfig.APPLICATION_ID);
ComponentName comp = new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.PermissionAndApps");
intent.setComponent(comp);
360手機(只能開啟到自帶安全軟體)
Intent intent = new Intent("android.intent.action.MAIN");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("packageName", BuildConfig.APPLICATION_ID);
ComponentName comp = new ComponentName("com.qihoo360.mobilesafe", "com.qihoo360.mobilesafe.ui.index.AppEnterActivity");
intent.setComponent(comp);
startActivity(intent);
由於資源和能力有限,只研究這些,其他廠家的適配,可以引導使用者到系統設定頁面,或者應用資訊(有些廠家會直接在應用資訊提供許可權管理入口);提一下,對於三星手機,嘗試過很多方法,但都沒能成功,自己專案中是直接引導使用者到應用資訊頁面。下面是兩個通用的方法,一個是引導至系統設定頁面,另一個引導至應用資訊頁面:
應用資訊介面
Intent localIntent = new Intent();
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= 9) {
localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
localIntent.setData(Uri.fromParts("package", getPackageName(), null));
} else if (Build.VERSION.SDK_INT <= 8) {
localIntent.setAction(Intent.ACTION_VIEW);
localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
localIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
}
startActivity(localIntent);
系統設定介面
Intent intent = new Intent(Settings.ACTION_SETTINGS);
startActivity(intent);
補充
1、手機的Build.MANUFACTURER,僅供參照,不排除個別廠家有作更改,有沒列到或者修正的歡迎小夥伴們留言,後續更新,謝謝
華為——Huawei
魅族——Meizu
小米——Xiaomi
索尼——Sony
oppo——OPPO
LG——LG
vivo——vivo
三星——samsung
樂視——Letv
中興——ZTE
酷派——YuLong
聯想——LENOVO
2、6.0許可權檢查示例程式碼
if (Build.VERSION.SDK_INT >= 23) {
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
// 沒有許可權
if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)){
//如果沒勾選“不再詢問”,向用戶發起許可權請求
ActivityCompat.requestPermissions((Activity) mContext, new String[]{Manifest.permission.CALL_PHONE}, 0);
}else{
//之前點選了“不再詢問”,無法再次彈出許可權申請框。
//可以給Toast提示,或者Dialog反饋給使用者,引導去開啟相應許可權
// 去應用資訊
Intent localIntent = new Intent();
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= 9) {
localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
localIntent.setData(Uri.fromParts("package", mContext.getPackageName(), null));
} else if (Build.VERSION.SDK_INT <= 8) {
localIntent.setAction(Intent.ACTION_VIEW);
localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
localIntent.putExtra("com.android.settings.ApplicationPkgName", mContext.getPackageName());
}
}
} else {
// 有許可權,接著你要乾的活
}
} else {
// 6.0之前的系統,因為無法獲取許可權的狀態,直接執行需要許可權的操作
}
3、小夥伴補充的,小米系統適配程式碼 感謝@name__repeated童鞋
String rom = getMiuiVersion();
Intent intent = null;
if (ROM_MIUI_V5.equals(rom)) {
Uri packageURI = Uri.parse("package:" + context.getApplicationInfo().packageName);
intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
} else if (ROM_MIUI_V6.equals(rom) || ROM_MIUI_V7.equals(rom)) {
intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
intent.putExtra("extra_pkgname", context.getPackageName());
} else {
}