Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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); startService(intent);

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

unbindService(mConnection); 

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() ??

edit

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

like image 620
geekoraul Avatar asked Dec 15 '13 07:12

geekoraul


2 Answers

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.

@Override public void onTaskRemoved(Intent rootIntent){     Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());     restartServiceIntent.setPackage(getPackageName());      PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);     AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);     alarmService.set(     AlarmManager.ELAPSED_REALTIME,     SystemClock.elapsedRealtime() + 1000,     restartServicePendingIntent);      super.onTaskRemoved(rootIntent);  } 

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.

like image 198
geekoraul Avatar answered Oct 05 '22 20:10

geekoraul


Please be aware of that: onDestroy is not always called. You should not put code that way.
When activity forced closed or closed by system abnormally, onDestroy is not getting called.

like image 32
Nick Jian Avatar answered Oct 05 '22 19:10

Nick Jian