geekoraul geekoraul - 1 year ago 104
Android Question

The process of the service is killed after the application is removed from the application tray

I am starting a service (or re-starting the running service) when an activity is launched, using :

Intent intent = new Intent(this, MyService.class);

Later on based on certain actions, the same activity binds to the service using

bindService(new Intent(this, MyService.class), mConnection, Context.BIND_AUTO_CREATE);

And when the activity is destroyed, I call


Earlier, the service used to restart when I killed the same activity/application from the application tray and showed the "message 1 process 1 service running" under running apps.

Now, the service does not restart on killing the same activity/application.

And I get the message "0 process 1 service running", which means the service is actually not running.

The service does not restart on application being closed. My application consists of one activity. Also the service is successfully started when launched after a system boot.

Why does the process of the service gets killed when I start it using startService() ??


The service used to re-start earlier after i closed the app from the application tray. But now suddenly with the SAME code, it doesn't. It happens with other apps too when i close them. eg.

enter image description here

Answer Source

Here is a workaround I came across and works well for re-starting a service if its process is killed on closing the application. In your service, add the following code.

I came across this workaround in this thread.

public void onTaskRemoved(Intent rootIntent){
    Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());

    PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    SystemClock.elapsedRealtime() + 1000,


Seems to be a bug that the process of the application is killed. There is no point for a service to run if its process is killed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download