Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Re-invoke application if task manager kill

Application thread get close if its killed by task manager. Need to re-invoke application as though its killed by other application or task manager. Any idea?

like image 426
arshad kr Avatar asked Sep 13 '12 06:09

arshad kr


People also ask

What happens when Android decides to shut down a process?

Android might decide to shut down a process at some point, when resources are required by other processes that are more immediately serving the user. Application components running in the process that's killed are consequently destroyed.

How to start service from activity in Android?

You can start a service from an activity or other application component by passing an Intent to startService() or startForegroundService() . The Android system calls the service's onStartCommand() method and passes it the Intent , which specifies which service to start.

How can we prevent a service from being killed by OS?

If you really needed Android not to kill your service, your best bet would be to make it a System App, or make your service return START_STICKY in your onStartCommand() method. This way, if your service gets killed, then it will be queued to restart automatically.


1 Answers

You have to run background service with START_STICKY command. Just extends Service and override onCommand like this :

@Override
public int onStartCommand(Intent intent,int flags,int startId) {
    super.onStartCommand(intent, flags, startId);

    return START_STICKY;
}

Like this your Service is restart when it's close (by system or anything else)

You just have now check on your service (onCreate for example) if application is running or not and launch it again if not. I suppose PackageManager let you check this or simply put a static boolean is_alive to see if your activity is always running.

Regards Jim

like image 78
jaumard Avatar answered Sep 18 '22 17:09

jaumard