需要提示MIUI用户开启某些权限,因此需要跳转其权限编辑页面
网上基本只有比较古老的MIUI5/6的跳转方式:
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);
这种方式已经不能满足如今的新版MIUI 8,所以,自己动手丰衣足食
电脑打开cmd/terminal, 手机打开应用权限设置页面:
命令行输入:
adb shell dumpsys activity | grep "mFocusedActivity"
输入结果:
mFocusedActivity: ActivityRecord{11ae2ef u0 com.miui.securitycenter/com.miui.permcenter.permissions.PermissionsEditorActivity t192}
即为当前打开的权限设置页的Activity
观察到Activity包名与MIUI 5/6的相同,Activity名字不同,将跳转代码改为:
// 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);
成功跳转。
完整的工具类如下:
public class MiuiUtils {
/**
* 跳转到MIUI应用权限设置页面
*
* @param context context
*/
public static void jumpToPermissionsEditorActivity(Context context) {
if (isMIUI()) {
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) {
// 否则跳转到应用详情
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", context.getPackageName(), null);
intent.setData(uri);
context.startActivity(intent);
}
}
}
}
/**
* 判断是否是MIUI
*/
private static boolean isMIUI() {
String device = Build.MANUFACTURER;
if (device.equals("Xiaomi")) {
try {
Properties prop = new Properties();
prop.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
return prop.getProperty("ro.miui.ui.version.code", null) != null
|| prop.getProperty("ro.miui.ui.version.name", null) != null
|| prop.getProperty("ro.miui.internal.storage", null) != null;
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
}
网友评论