概述
最近项目中遇到这样个需求场景:<br > 当我们的用户使用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 {
}
2017/9/1补充MIUI8,感谢@fighting_goat童鞋
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 if(ROM_MIUI_V8.equals(rom)){
intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
intent.putExtra("extra_pkgname", context.getPackageName());
}else{
}
网友评论
可以使用adb shell dumpsys activity top获取包名+类名,但是,假如厂商把类名改了怎么办,比如miui7/8,有没有什么更简单的方法获取
val intent = Intent()
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
val comp = ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity")
intent.component = comp
context.startActivity(intent)
手机型号是魅蓝 note2
Android 5.1
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("pkg_name", activity.getPackageName());
intent.putExtra("app_name", activity.getString(R.string.app_name));
intent.putExtra("class_name", "com.example.ActivityWork");
ComponentName comp = new ComponentName("com.coloros.notificationmanager", "com.coloros.notificationmanager.AppDetailPreferenceActivity");
intent.setComponent(comp);
activity.startActivity(intent);
Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
ComponentName componentName = new ComponentName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
intent.setComponent(componentName);
intent.putExtra("extra_pkgname", activity.getPackageName());
activity.startActivity(intent);
8以下把类名最后加上App
if (Build.VERSION.SDK_INT >= 23) {
if (ActivityCompat.checkSelfPermission(mContext, permission) != 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之前的系统,因为无法获取权限的状态,直接执行需要权限的操作
}
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 {
}
魅族——Meizu
小米——Xiaomi
索尼——Sony
oppo——OPPO
LG——LG
vivo——vivo
三星——samsung
乐视——Letv
中兴——ZTE
酷派——YuLong
联想——LENOVO