最近项目上有个需求,读取通讯录。当用户点了拒绝访问通讯录或者其他权限,导致无法使用,这时候我想重新打开权限设置,但是对于很多小白用户不知道怎么设置,这就会导致用户体验不友好的一面。
之前已经有人写过类似的文章,不过都比较分散,经实测将这些方法总结了一下。
要跳转的权限设置界面如图:目前手上只有华为和小米作为测试
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 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
/* 判断手机是否是魅族设备,判断代码源自魅族消息推送的SDK https://open-res.flyme.cn/fileserver/upload/file/202109/1c143468d01f4b5ea1dbb362b5d07d30.zip */ public static boolean isMeizu() { String prop = getSystemProperty("ro.meizu.product.model", ""); if (!TextUtils.isEmpty(prop)) { return true; } prop = getSystemProperty("ro.vendor.meizu.product.model", ""); if (!TextUtils.isEmpty(prop)) { return true; } boolean equals = "meizu".equalsIgnoreCase(android.os.Build.BRAND); if (equals) { return true; } equals = "魅蓝".equalsIgnoreCase(Build.BRAND); if (equals) { return true; } return "22c4185e".equalsIgnoreCase(Build.BRAND); } public static String getSystemProperty(String property, String defVal) { try { @SuppressLint("PrivateApi") final Class<?> clazz = Class.forName("android.os.SystemProperties"); final Method getter = clazz.getDeclaredMethod("get", String.class); final String value = (String) getter.invoke(null, property); if (!TextUtils.isEmpty(value)) { return value; } } catch (Exception ignored) { } return defVal; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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 (isMeizu())) { gotoMeizuPermission(); } else if (TextUtils.equals(brand.toLowerCase(), "huawei") || TextUtils.equals(brand.toLowerCase(), "honor")) { gotoHuaweiPermission(); } else { startActivity(getAppDetailSettingIntent()); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
/** * 跳转到miui的权限管理页面 */ private void gotoMiuiPermission() { try { // MIUI 8 final Intent 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()); context.startActivity(intent); } catch (Exception e) { try { // MIUI 5/6/7 final Intent 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()); context.startActivity(intent); } catch (Exception e1) { // 否则跳转到应用详情 startActivity(getAppDetailSettingIntent()); } } } /** * 跳转到魅族的权限管理系统 */ private void gotoMeizuPermission() { try { final 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 { final Intent intent = new Intent(); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); final 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() { final Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS"); intent.setData(Uri.fromParts("package", getPackageName(), null)); } else { intent.setAction(Intent.ACTION_VIEW); intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails"); intent.putExtra("com.android.settings.ApplicationPkgName", getPackageName()); } return intent; } |