Restarting a Foreground Service in android - android

I have a service, which runs in a foreground. I understand that if a Foreground service is killed by the system, it's restarted after the availability of resources.
What I want to know is, if it will be restarted, even if the Foreground Service is manually killed from 'Running Services' section of 'Settings'?

If you want the service to restart after being killed use this method in the service object:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}

Related

Will forcely killing Android sticky service will restart it again?

I'm forcely killing my Android Sticky service from "Running Services". But its not getting start again by the OS automatically as mentioned here.
1 - How soon it is restarted again when forcily closed by user through Running Services?
2 - How soon it is restarted again when OS destroys the sticky service?
So from my experience the answer is NO the OS doesn't call the service again if its forcefully shut down by user via "Running Services". It only calls the service onStartCommand(Intent intent, int flags, int startId) method when application is destroyed (swipe away).
But I found a quick solution, as we know when service is destroyed the onDestroy() method is called. So just initialize the service again like this:
#Override
public void onDestroy() {
super.onDestroy();
startService(new Intent(context, TimerService.class));
Log.v("App", "Service Started Again");
}

background services for getting activity recognition

I read a lot about services I tried a lot of examples but unfortunately I couldn't understand how could I keep always services running to get activity recognition API always up and running.
In all cases Android OS killing somehow my running service.
What I tried:
I put START_STICKY but when it killed never runs again
#Override
public int onStartCommand(Intent intent, final int flags, int startId) {
// my code is here
return START_STICKY;
}
I started service again when I detected that service was killed. It not worked good as expected. I am sending broadcast and starting the service again.
#Override
public void onDestroy() {
sendBroadcast(new Intent("YouWillNeverKillMe"));
super.onDestroy();
}
I know about foreground services but in this case there is no way I can use foreground services. I have to forget about this solution.
Is there any other way you can suggest me as a solution? What exactly doing there Facebook or Viber that always I getting messages?

Is is possible to run Android Service after app deinstallation?

i want to run a script even if the Task is destroyed. That works fine but is it possible to keep this service running, after the user destroyes the app?
I read something about binding the service but this is not working for me.
No. If the user uninstalls the app, all components are destroyed and removed from the operating system.
However, you can make a Service automatically restart after the app is killed (but not uninstalled) by starting through context.startService(Intent), and returning Service.START_STICKY in onStartCommand()
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}

Prevent Android App in background getting suspended

I wrote an App that will run process in a long time, possibly never ending until the user stop it.
But I afraid the App will get suspended when the mobile/tablet screen is sleep, or user switch to another app so that my app long running task is getting suspended.
How do I prevent that?
You can probably start a Service here if you want your Application to run in Background. This is what Service in Android are used for - running in background and doing longtime operations.
You can use START_STICKY to make your Service running continuously.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
and see this :http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle
and This is an example of background service:
http://marakana.com/forums/android/examples/60.html
What you want is implement a Service.
It will have that behavior that you are asking.
You should use a Service and return START_STICKY on the onStartCommand() so that the system will not terminate your app. Even if it is terminated, the service is automatically restarted.

onDestory () in service in Android

I would like to to restart my service whenever it is stopped by Android or task killers?
For which I am trying start_Sticky using the below code in onStartCommand():
#Override
public int onStartCommand(Intent intent,int flags,int startId)
{
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
This works fine for Android versions below 4.4.2. Hence, I thought I can restart the service whenever the OnDestroy() is called in the service?
Is this possible or is there any other workaround for 4.4.2 version?
When is OnDestroy in Service is called?
Thanks!
set an alarmanager to call the service after particular interval of time regularly.If service is all ready running then no need to start once again else start the service.
OnDestroy will be called when you kill the Service.However, it will not be called,when you kill the progress.
Keeping service alive is a big problem.

Categories

Resources