Android跳转权限设置页面

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

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

 


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


MIUI:



华为:



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

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

public static final String BOARDThe name of the underlying board, like "goldfish".主板名称
public static final String BOOTLOADERThe system bootloader version number.系统引导程序版本号
public static final String BRANDThe brand (e.g., carrier) the software is customized for, if any.android系统定制商
public static final String CPU_ABIThe name of the instruction set (CPU type + ABI convention) of native code.CPU 和ABI的本地代码指令集
public static final String CPU_ABI2The name of the second instruction set (CPU type + ABI convention) of native code.
public static final String DEVICEThe name of the industrial design.设备参数
public static final String DISPLAYA build ID string meant for displaying to the user显示屏参数
public static final String FINGERPRINTA string that uniquely identifies this build.硬件名
public static final String HARDWAREThe name of the hardware (from the kernel command line or /proc).内核命令行中的硬件名
public static final String HOST
public static final String IDEither a changelist number, or a label like "M4-rc20".修改版本列表
public static final String MANUFACTURERThe manufacturer of the product/hardware.硬件厂商
public static final String MODELThe end-user-visible name for the end product.版本
public static final String PRODUCTThe name of the overall product.手机厂商
public static final String RADIOThis 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 SERIALA hardware serial number, if available.
public static final String TAGSComma-separated tags describing the build, like "unsigned,debug".描述Build的标签
public static final long TIME
public static final String TYPEThe type of build, like "user" or "eng".Build的类型
public static final String USER
通过这些常量就可以获得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;
    }


  • 7
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值