android - Autostart permission programmatically
问题描述
I am working on an app where I need to ask user for the autostart permission and for that I am opening the Autostart permissions settings page for the user to turn on the permission for our app using following code for few Manufacturers:
Intent autostartIntent = new Intent();
if ("xiaomi".equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
autostartIntent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
startActivity(autostartIntent);
} else if ("oppo".equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
autostartIntent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"));
startActivity(autostartIntent);
} else if ("vivo".equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
autostartIntent.setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
startActivity(autostartIntent);
} else if ("huawei".equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
autostartIntent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));
startActivity(autostartIntent);
}
Moreover, when I am trying to redirect user to the following devices, I am facing following difficulties:
On RealMe 2 Pro, for which the manufacturer is Oppo, the system is unable to start the AutoStart Permissions Activity.
On Moto and Nokia devices, I am not able to get the path of AutoStart Activity, so that I can redirect user to that page directly.
解决方案
Found the solution to this question, i am opening the Battery Optimization page for the user to turn off Battery Optimization for my app.
Here is the code i am using:
AlertDialog.Builder builder = new AlertDialog.Builder(PermissionsActivity.this);
builder.setTitle("Turn off Battery Optimization");
builder.setMessage("Find XYZ in the list of applications and choose ‘Don’t optimize’.");
builder.setPositiveButton("Allow",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (Build.VERSION.SDK_INT >= 23) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
startActivity(intent);
}
}
});
builder.setNegativeButton("Deny", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
builder.show();
推荐阅读
- php - Php Elastic Search 按地理距离排序
- javascript - Kendo Grid 在每一行上创建一个图标按钮
- r - 在 R 3.6.3(2020 年 10 月)中安装 data.table 时出现问题
- python - 如何使用 spark-shell 或 spark-submit 读取 hdfs 中的磁盘映像文件
- android - 在 Android 中使用 ScrollView 时有太多重复的文本
- java - 代码正在运行,但总价计算错误
- ionic-framework - 离子本地通知
- c# - Pomelo C#:我无法导入没有数据的表
- pandas - 匹配来自两个数据框的买入卖出条目并创建一个新的。蟒蛇 3.8 / W10
- r - R中的线图