Android跳转权限设置页面

最近项目上有个需求,读取通讯录。当用户点了拒绝访问通讯录或者其他权限,导致无法使用,这时候我想重新打开权限设置,但是对于很多小白用户不知道怎么设置,这就会导致用户体验不友好的一面。

之前已经有人写过类似的文章,不过都比较分散,经实测将这些方法总结了一下。

 


要跳转的权限设置界面如图:目前手上只有华为和小米作为测试


MIUI:

《Android跳转权限设置页面》

华为:

《Android跳转权限设置页面》


上代码前先整理下关于Build类的相关调用,后面有可能用到:

在官方文档中可以查到Build类中有如下常量:

public static final StringBOARDThe name of the underlying board, like “goldfish”.主板名称
public static final StringBOOTLOADERThe system bootloader version number.系统引导程序版本号
public static final StringBRANDThe brand (e.g., carrier) the software is customized for, if any.android系统定制商
public static final StringCPU_ABIThe name of the instruction set (CPU type + ABI convention) of native code.CPU 和ABI的本地代码指令集
public static final StringCPU_ABI2The name of the second instruction set (CPU type + ABI convention) of native code.
public static final StringDEVICEThe name of the industrial design.设备参数
public static final StringDISPLAYA build ID string meant for displaying to the user显示屏参数
public static final StringFINGERPRINTA string that uniquely identifies this build.硬件名
public static final StringHARDWAREThe name of the hardware (from the kernel command line or /proc).内核命令行中的硬件名
public static final StringHOST
public static final StringIDEither a changelist number, or a label like “M4-rc20”.修改版本列表
public static final StringMANUFACTURERThe manufacturer of the product/hardware.硬件厂商
public static final StringMODELThe end-user-visible name for the end product.版本
public static final StringPRODUCTThe name of the overall product.手机厂商
public static final StringRADIOThis 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 StringSERIALA hardware serial number, if available.
public static final StringTAGSComma-separated tags describing the build, like “unsigned,debug”.描述Build的标签
public static final longTIME
public static final StringTYPEThe type of build, like “user” or “eng”.Build的类型
public static final StringUSER

通过这些常量就可以获得Android手机的一些设备信息。

       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; }
    原文作者:发型不给力
    原文地址: https://blog.csdn.net/luckrr/article/details/78211465
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞