最近项目上有个需求,读取通讯录。当用户点了拒绝访问通讯录或者其他权限,导致无法使用,这时候我想重新打开权限设置,但是对于很多小白用户不知道怎么设置,这就会导致用户体验不友好的一面。
之前已经有人写过类似的文章,不过都比较分散,经实测将这些方法总结了一下。
要跳转的权限设置界面如图:目前手上只有华为和小米作为测试
MIUI:
华为:
上代码前先整理下关于Build类的相关调用,后面有可能用到:
在官方文档中可以查到Build类中有如下常量:
public static final String | BOARD | The name of the underlying board, like "goldfish".主板名称 |
public static final String | BOOTLOADER | The system bootloader version number.系统引导程序版本号 |
public static final String | BRAND | The brand (e.g., carrier) the software is customized for, if any.android系统定制商 |
public static final String | CPU_ABI | The name of the instruction set (CPU type + ABI convention) of native code.CPU 和ABI的本地代码指令集 |
public static final String | CPU_ABI2 | The name of the second instruction set (CPU type + ABI convention) of native code. |
public static final String | DEVICE | The name of the industrial design.设备参数 |
public static final String | DISPLAY | A build ID string meant for displaying to the user显示屏参数 |
public static final String | FINGERPRINT | A string that uniquely identifies this build.硬件名 |
public static final String | HARDWARE | The name of the hardware (from the kernel command line or /proc).内核命令行中的硬件名 |
public static final String | HOST | |
public static final String | ID | Either a changelist number, or a label like "M4-rc20".修改版本列表 |
public static final String | MANUFACTURER | The manufacturer of the product/hardware.硬件厂商 |
public static final String | MODEL | The end-user-visible name for the end product.版本 |
public static final String | PRODUCT | The name of the overall product.手机厂商 |
public static final String | RADIO | This field was deprecated in API level 14. The radio firmware version is frequently not available when this class is initialized, leading to a blank or "unknown" value for this string. UsegetRadioVersion() instead. |
public static final String | SERIAL | A hardware serial number, if available. |
public static final String | TAGS | Comma-separated tags describing the build, like "unsigned,debug".描述Build的标签 |
public static final long | TIME | |
public static final String | TYPE | The type of build, like "user" or "eng".Build的类型 |
public static final String | USER |
String sdk = android.os.Build.VERSION.SDK; // SDK号
String model = android.os.Build.MODEL; // 手机型号
String release = android.os.Build.VERSION.RELEASE; // android系统版本号
String brand = Build.BRAND;//手机厂商
if (TextUtils.equals(brand.toLowerCase(), "redmi") || TextUtils.equals(brand.toLowerCase(), "xiaomi")) {
gotoMiuiPermission();//小米
} else if (TextUtils.equals(brand.toLowerCase(), "meizu")) {
gotoMeizuPermission();
} else if (TextUtils.equals(brand.toLowerCase(), "huawei") || TextUtils.equals(brand.toLowerCase(), "honor")) {
gotoHuaweiPermission();
} else {
startActivity(getAppDetailSettingIntent());
}
/**
* 跳转到miui的权限管理页面
*/
private void gotoMiuiPermission() {
try { // MIUI 8
Intent localIntent = new Intent("miui.intent.action.APP_PERM_EDITOR");
localIntent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
localIntent.putExtra("extra_pkgname", context.getPackageName());
context.startActivity(localIntent);
} catch (Exception e) {
try { // MIUI 5/6/7
Intent localIntent = new Intent("miui.intent.action.APP_PERM_EDITOR");
localIntent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
localIntent.putExtra("extra_pkgname", context.getPackageName());
context.startActivity(localIntent);
} catch (Exception e1) { // 否则跳转到应用详情
startActivity(getAppDetailSettingIntent());
}
}
}
/**
* 跳转到魅族的权限管理系统
*/
private void gotoMeizuPermission() {
try {
Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.putExtra("packageName", BuildConfig.APPLICATION_ID);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
startActivity(getAppDetailSettingIntent());
}
}
/**
* 华为的权限管理页面
*/
private void gotoHuaweiPermission() {
try {
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity");//华为权限管理
intent.setComponent(comp);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
startActivity(getAppDetailSettingIntent());
}
}
/**
* 获取应用详情页面intent(如果找不到要跳转的界面,也可以先把用户引导到系统设置页面)
*
* @return
*/
private Intent getAppDetailSettingIntent() {
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());
}
return localIntent;
}