Search code examples
androidserviceandroid-studio-3.0background-service

The service does not run if the app is killed


So I have a service that checks if the Screen is ON or OFF (from here). This is the main activity:

public class MainActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onCreate();
    }
    public void onCreate() {

        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        BroadcastReceiver mReceiver = new ScreenReceiver();
        registerReceiver(mReceiver, filter);
    }

    @Override
    protected void onPause() {
        if (ScreenReceiver.screenOff) {

            System.out.println("SCREEN TURNED OFF");
        } else {

            System.out.println("this is when onPause() is called when the screen state has not changed ");

        }
        super.onPause();
    }
    @Override
    protected void onResume() {
        // only when screen turns on
        if (!ScreenReceiver.screenOff) {
            System.out.println("SCREEN TURNED ON");
        } else {

            System.out.println(" this is when onResume() is called when the screen state has not changed ");
        }
        super.onResume();
    }
}

The Service class:

public class UpdateService extends Service {

    public void onCreate() {
        super.onCreate();
        // register receiver that handles screen on and screen off logic

        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        BroadcastReceiver mReceiver = new ScreenReceiver();
        registerReceiver(mReceiver, filter);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        boolean screenOn = intent.getBooleanExtra("screen_state", false);
        if (!screenOn) {
            System.out.println("Screen is off");
        } else {
            System.out.println("Screen is on");
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

Receiver:

public class ScreenReceiver extends BroadcastReceiver {

    public static boolean screenOff;

    @Override
    public void onReceive(Context context, Intent intent) {

        System.out.println("onReceive ");
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            screenOff = true;
            System.out.println("SCREEN TURNED OFF on BroadcastReceiver");
            Log.d("Check", "SCREEN TURNED OFF" );
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            screenOff = false;
            System.out.println("SCREEN TURNED ON on BroadcastReceiver");
            Log.d("Check", "SCREEN TURNED ON" );

        }
        Intent i = new Intent(context, UpdateService.class);
        i.putExtra("screen_state", screenOff);
        context.startService(i);
    }
}

The above service runs perfectly when the app is sent to the background. But when the app is killed using the task manager, the service does not work anymore. I need the service to work even when the app is killed.

What should I do to enable that? Any ideas?


Solution

  • The reason is that your service is running on the same process as the application. If you set android:process=":yourServiceName" in the manifest file, your service will start a new process and you can see your service.

    Also, do check if your service is actually running using this method

    private boolean isMyServiceRunning(Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
    

    To restart the service you can use an AlarmManager inside your onTaskRemoved method of the service.

    @Override
        public void onTaskRemoved(Intent rootIntent) {
    
            Intent restartService = new Intent(getApplicationContext(), this.getClass());
            PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartService, PendingIntent.FLAG_ONE_SHOT);
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarmManager.set(AlarmManager.ELAPSED_REALTIME, 5000, pendingIntent);
    
    
        }