我正在开发一个应用程序,其中一项服务需要始终运行以在后台执行某些特定操作。因此,无论何时从任务管理器中删除服务时,我都会使用广播接收器重新启动该服务。因此,为此,我从用户处获得了自动启动/电池优化权限,以使其重新启动。
除了在ColorOS上,几乎所有领先的设备制造商都可以使用这种方法,并且只要为我的应用程序启用了“自动启动/电池优化许可”,在ColorOS上的所有其他设备上,该方法都可以使用。
原因是,我无法将用户重定向到“自动启动”或“电池优化”设置页面
我尝试使用以下代码从我的应用程序打开“自动启动设置”活动:
Intent autostartIntent = new Intent();
autostartIntent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"));
startActivity(autostartIntent);
我也尝试过手动设置省电设置,以检查是否在任何情况下都有效。但是无论如何似乎没有任何效果。
我正在寻找一种将用户重定向到自动启动权限页面或电池优化设置页面的方法。任何处理过类似问题的人都可以提出一些解决方案,甚至可以解决相同的问题。
得到它的工作!
我已将用户重定向到应用程序详细信息页面,他/她需要在其中打开“自动启动”选项。这将使服务在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,因此我在收到新通知时重新启动服务,因此,每次出现新通知时,它都会唤醒我需要始终保持运行的服务。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句