ANDROID跳转到手机自启动管理页面

前言

有时候,为了确保APP的后台正常运行,需要让用户去设置打开APP的自启动权限(或者改为手动管理/huawei)。为了更好的用户体验,在APP中可以直接跳转到手机的自启动管理页面或者权限管理页面。


应用启动管理页面

oppo/vivo是没有这个页面的
华为

  • 华为
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
ComponentName componentName = null;
int sdkVersion = Build.VERSION.SDK_INT;
try {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//跳自启动管理
if (sdkVersion >= 28){//9:已测试
componentName = ComponentName.unflattenFromString("com.huawei.systemmanager/.startupmgr.ui.StartupNormalAppListActivity");//跳自启动管理
}else if (sdkVersion >= 26){//8:已测试
componentName = ComponentName.unflattenFromString("com.huawei.systemmanager/.appcontrol.activity.StartupAppControlActivity");
}else if (sdkVersion >= 23){//7.6:已测试
componentName = ComponentName.unflattenFromString("com.huawei.systemmanager/.startupmgr.ui.StartupNormalAppListActivity");
}else if (sdkVersion >= 21){//5
componentName = ComponentName.unflattenFromString("com.huawei.systemmanager/com.huawei.permissionmanager.ui.MainActivity");
}
//componentName = new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity");//锁屏清理
intent.setComponent(componentName);
startActivity(intent);
}catch (Exception e){
//跳转失败
}
123456789101112131415161718192021
  • 小米
1
2
3
4
5
6
7
8
9
10
11
Intent intent = new Intent();
try {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName componentName = null;
componentName = new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity");
intent.setComponent(componentName);
context.startActivity(intent);
} catch (Exception e) {//抛出异常就直接打开设置页面
jumpAPPInfo(context);
}
12345678910
  • 三星
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Intent intent = new Intent();
ComponentName componentName = null;
try {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//跳自启动管理
componentName = ComponentName.unflattenFromString("com.samsung.android.sm/.app.dashboard.SmartManagerDashBoardActivity");
intent.setComponent(componentName);
startActivity(intent);
}catch (Exception e){
try {
componentName = new ComponentName("com.samsung.android.sm_cn", "com.samsung.android.sm.ui.ram.AutoRunActivity");
intent.setComponent(componentName);
startActivity(intent);
}catch (Exception e1){
//跳转失败
}
}
1234567891011121314151617
  • 魅族
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Intent intent = new Intent();
ComponentName componentName = null;
try {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//跳自启动管理
componentName = ComponentName.unflattenFromString("com.meizu.safe/.permission.SmartBGActivity");//跳转到后台管理页面
intent.setComponent(componentName);
startActivity(intent);
}catch (Exception e){
try {
componentName = ComponentName.unflattenFromString("com.meizu.safe/.permission.PermissionMainActivity");//跳转到手机管家
intent.setComponent(componentName);
startActivity(intent);
}catch (Exception e1){
//跳转失败
}
}
1234567891011121314151617

权限管理页面

这个页面重要的权限自启动、锁屏显示、后台弹出界面

vivo

  • vivo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Intent localIntent;
try {
if (((Build.MODEL.contains("Y85")) && (!Build.MODEL.contains("Y85A"))) || (Build.MODEL.contains("vivo Y53L"))) {
localIntent = new Intent();
localIntent.setClassName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.PurviewTabActivity");
localIntent.putExtra("packagename", context.getPackageName());
localIntent.putExtra("tabId", "1");
context.startActivity(localIntent);
} else {
localIntent = new Intent();
localIntent.setClassName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.SoftPermissionDetailActivity");
localIntent.setAction("secure.intent.action.softPermissionDetail");
localIntent.putExtra("packagename", context.getPackageName());
context.startActivity(localIntent);
}
}catch (Exception e){
// 否则跳转到应用详情
localIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", context.getPackageName(), null);
localIntent.setData(uri);
context.startActivity(localIntent);
}
12345678910111213141516171819202122
  • OPPO
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
Intent intent = new Intent();
try {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("packageName", BuildConfig.APPLICATION_ID);
ComponentName comp = new ComponentName("com.color.safecenter", "com.color.safecenter.permission.PermissionManagerActivity");
intent.setComponent(comp);
startActivity(intent);
}catch (Exception e){
try {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("pkg_name", context.getPackageName());
intent.putExtra("app_name", context.getString(R.string.app_name));
intent.putExtra("class_name", "com.welab.notificationdemo.MainActivity");
ComponentName comp = new ComponentName("com.coloros.notificationmanager", "com.coloros" +
".notificationmanager.AppDetailPreferenceActivity");
intent.setComponent(comp);
startActivity(intent);
}catch (Exception e1){
// 否则跳转到应用详情
intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", context.getPackageName(), null);
intent.setData(uri);
context.startActivity(intent);
}
}
12345678910111213141516171819202122232425
  • 小米
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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);
}
}
123456789101112131415161718192021

应用详情页面

1
2
3
4
5
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", context.getPackageName(), null);
intent.setData(uri);
startActivity(intent);
1234

总结

在其他应用上层显示、后台弹出界面、锁屏显示这三个权限会直接影响APP从后台启动活动,详情可以了解另一篇文章–>Android 在后台无法启动Activity

附加

vivo/oppo/小米手机有一个“后台弹出界面”权限, 华为是没有的, 可以使用以下代码来检测该权限是否开启:

1
2
3
4
5
6
7
8
9
10
11
12
13
private boolean isAllowed() {
AppOpsManager ops = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
try {
int op = 10021;
Method method = ops.getClass().getMethod("checkOpNoThrow", new Class[]{int.class, int.class, String.class});
Integer result = (Integer) method.invoke(ops, op, Process.myUid(), getPackageName());
return result == AppOpsManager.MODE_ALLOWED;

} catch (Exception e) {
Log.e(TAG, "not support");
}
return false;
}

本文转载自:https://www.freesion.com/article/7753459867/

乱码三千 – 点滴积累 ,欢迎来到乱码三千技术博客站

0%