从ColorOS上的任务管理器中删除应用程序后,后台服务被杀死

问题描述 投票:2回答:1

我正在开发一个应用程序,其中一个服务需要始终运行以在后台执行某些特定操作。所以当我从任务管理器中杀死时,我正在使用广播接收器重新启动服务。因此,我正在从用户那里获得Autostart / Battery Optimization权限,以便重新启动它。

除了ColorOS之外,几乎所有领先的设备制造商都采用这种方法完全正常,只要为我的应用程序打开“自动启动/电池优化权限”,它就可以在除ColorOS之外的所有其他设备上完全正常工作。

原因是,我无法将用户重定向到“AutoStart”或“Battery Optimization”设置页面

我尝试使用以下代码从我的应用中打开自动启动设置活动:

Intent autostartIntent = new Intent();
autostartIntent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"));
startActivity(autostartIntent);

此外,我尝试手动播放节电设置,以检查是否有效。但无论如何似乎没有任何工作。

我想找到一种方法将用户重定向到自动启动权限页面或电池优化设置页面。处理类似问题的任何人都可以提出一些解决方案,甚至是相同的解决方法。

android service background-service
1个回答
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,它始终保持运行,因此我在收到新通知时重新启动服务,因此每次出现新通知时,它都会唤醒我需要始终保持运行的服务。

© www.soinside.com 2019 - 2024. All rights reserved.