我正在开发一个需要始终运行一项服务才能在后台执行某些特定操作的应用程序。因此,无论何时从任务管理器中删除服务时,我都会使用广播接收器重新启动该服务。因此,为此,我获得了用户的“自动启动/电池优化”权限,以使其重新启动。
除 ColorOS 以外,几乎所有领先的设备制造商都可以使用这种方法,并且只要我的应用程序启用了“自动启动/电池优化许可”,它就可以正常使用除ColorOS上的其他设备外。
原因是,我无法将用户重定向到“自动启动”或“电池优化”设置页面
我尝试使用以下代码从我的应用程序打开“自动启动设置”活动:
Intent autostartIntent = new Intent();
autostartIntent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"));
startActivity(autostartIntent);
我还尝试手动使用省电设置来检查在任何情况下是否都有效。但是似乎仍然没有任何效果。
我正在寻找一种将用户重定向到“自动启动”页面或电池优化设置页面的方法。处理类似类型问题的任何人都可以针对该问题提出一些解决方案甚至解决方法。
答案 0 :(得分:0)
让它正常工作!
我已将用户重定向到应用程序详细信息页面,他/她需要在其中打开“自动启动”选项。这将使服务在ColorOS上运行。下面是将用户重定向到应用程序详细信息页面的代码,在这里用户需要打开自动启动功能。
if ("oppo".equalsIgnoreCase(Build.MANUFACTURER)) {
AlertDialog.Builder builder = new AlertDialog.Builder(PermissionsActivity.this);
builder.setTitle("Allow Auto-startup");
builder.setMessage("To access content on lock screen, enable ‘Allow Auto-startup’ for JiffyFeed");
builder.setPositiveButton("Allow",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", getPackageName(), null));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
builder.setNegativeButton("Deny", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Do something
}
});
builder.show();
}
此外,我也使用了替代方法。我使用的是 NotificationListenerService ,它始终保持运行,因此我在收到新通知时重新启动服务,因此每次出现新通知时,它都会唤醒我需要继续运行的服务总是。