Search code examples
androidservicebackground-service

Background service getting killed on removing the app from task manager on ColorOS


I am working on an app where one of the service needs to be running always to perform some specific operation in the background. So i am restarting the service using a Broadcast Receiver, whenever it is being killed from the task manager. So for this i am taking Autostart/Battery Optimization permission from the user to get it restarted again.

This approach is working completely fine with almost every leading device manufacturers except on ColorOS and as long as the "Autostart/Battery Optimization permission" is turned on for my app, it is working completely fine on every other devices except that on ColorOS.

The reason for this being, i am not able to redirect user to the "AutoStart" or "Battery Optimization" settings page

I have tried to open the Autostart settings Activity from my app using this code:

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

Also i have tried to play with the power saving settings manually to check if in any case that is working. But nothing seems to be working anyways.

I would be looking a way to redirect user to the Autostart permission page or to the battery optimization settings page. Anyone who dealt with the similar kind of problem can suggest some solution or even workarounds for the same.


Solution

  • Got it working!!

    I have redirected user to the app details page and there he/she need to turn on Auto Startup option. This will keep the service running on ColorOS. Below is the code to redirect user to the app details page and here the user need to turn the Auto Startup on.

    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();
    }
    

    Additionally, i have used a workaround as well. I am using NotificationListenerService which keeps on running always, so i am restarting the service on receipt of a new notification, so every time a new notification comes up, it wakes up the service which i need to keep running always.