参考博客:Android跳转权限设置页面
本文基于上面博客进行封装,直接上代码:
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;import com.example.geigei_android.BuildConfig;/*** author:zjp* date:2019/5/21*/
public class PermissionUtil {public static void gotoPermission(Context context) {String brand = Build.BRAND;//手机厂商if (TextUtils.equals(brand.toLowerCase(), "redmi") || TextUtils.equals(brand.toLowerCase(), "xiaomi")) {PermissionUtil.gotoMiuiPermission(context);//小米} else if (TextUtils.equals(brand.toLowerCase(), "meizu")) {PermissionUtil.gotoMeizuPermission(context);} else if (TextUtils.equals(brand.toLowerCase(), "huawei") || TextUtils.equals(brand.toLowerCase(), "honor")) {PermissionUtil.gotoHuaweiPermission(context);} else {context.startActivity(PermissionUtil.getAppDetailSettingIntent(context));}}/*** 跳转到miui的权限管理页面*/private static void gotoMiuiPermission(Context context) {try { // MIUI 8Intent 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/7Intent 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) { // 否则跳转到应用详情context.startActivity(getAppDetailSettingIntent(context));}}}/*** 跳转到魅族的权限管理系统*/private static void gotoMeizuPermission(Context context) {try {Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");intent.addCategory(Intent.CATEGORY_DEFAULT);intent.putExtra("packageName", BuildConfig.APPLICATION_ID);context.startActivity(intent);} catch (Exception e) {e.printStackTrace();context.startActivity(getAppDetailSettingIntent(context));}}/*** 华为的权限管理页面*/private static void gotoHuaweiPermission(Context context) {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);context.startActivity(intent);} catch (Exception e) {e.printStackTrace();context.startActivity(getAppDetailSettingIntent(context));}}/*** 获取应用详情页面intent(如果找不到要跳转的界面,也可以先把用户引导到系统设置页面)*/private static Intent getAppDetailSettingIntent(Context context) {Intent localIntent = new Intent();localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));return localIntent;}
}
可直接复用,新建PermissionUtil.java然后将代码拷贝进去即可使用,使用方法:
当检测到权限不足时:
调用该方法即可,笔者测试过,红米,荣耀手机均可运行,能够成功跳转至权限设置界面。