Till Android 6.0 whenever I Kill my app my service gets restarted automatically, but when I tested it in 7.1.1 my service does not gets restart it stops.
I tried restarting the service in activity onDestroy like this,
#Override
protected void onDestroy() {
super.onDestroy();
Util.printMessage("onDestroy.....");
Intent locationIntent = new Intent(getApplicationContext(), SupportService.class);
startService(locationIntent);
Util.printMessage("running...?..." + isMyServiceRunning(SupportService.class));
}
But this solution did not work, can anyone suggest what is the issue or how to start the service again and same for the boot change receiver my broadcast does not get call when device is rebooted.
Change the return type of onStartCommand() with START_STICKY. if the service destroy by system it will start automatically... for more info about START_STICKY Click
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?
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.
I would like to start my foreground service when my application is closed.
I tryed OnStop() but it's not a good idea for me because it can trigger multiple times and i which it to run only one instance.
I tryed OnDestroy() but it's simply doesn't trigger since i'm only using one activity in my whole app and most of time it is being kill with the SWIPE.
Is there a way i can detect when my application being kill or close ?
Thanks!
Only one instance of the service will run no matter how many times you start it. Each time a client starts the service the onStartCommand method fires. return Service.START_STICKY; to have your service stay running in the back ground after your app exits. But be warned if things get busy and the phone needs memory your service will be killed and you'll have to restart it like #Onur suggests with a conservative periodic AlarmManager intent.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// the service is started so after all clients are unbound it stays
// running
return Service.START_STICKY;
}
You can add your services description in manifest.xml stopWithTask="false" and in your sevice override the onTaskRemoved (Intent rootIntent) to know when activity that started the service is stopped for API level 14 and later.
Or you can set an alarm for some periods to check if your application is still running using AlarmManager. You should be careful with this tho, because it might consume battery based on the period you choose.
I am referring to android design considerations: AsyncTask vs Service (IntentService?)
According to the discussion, AsyncTask does not suit, because it is tightly "bound" to your Activity
So, I launch a Thread (I assume AsyncTask and Thread belong to same category), have an infinity running loop in it and did the following testing.
I quit my application, by keeping pressing on back soft key, till I saw home screen. Thread is still running.
I kill my application, by going to Manage apps -> App -> Force stop. Thread is stopped.
So, I expect after I change from Thread to Service, my Service will keep alive even after I quit or kill my app.
Intent intent = new Intent(this, SyncWithCloudService.class);
startService(intent);
public class SyncWithCloudService extends IntentService {
public SyncWithCloudService() {
super("SyncWithCloudService");
}
#Override
protected void onHandleIntent(Intent intent) {
int i = 0;
while (true) {
Log.i("CHEOK", "Service i is " + (i++));
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Log.i("CHEOK", "", ex);
}
}
}
}
// Doesn't matter whether I use "android:process" or not.
<service
android:name="com.xxx.xml.SyncWithCloudService"
android:process=".my_process" >
</service>
However, my finding is that,
I quit my application, by keeping pressing on back soft key, till I saw home screen. Service is still running.
I kill my application, by going to Manage apps -> App -> Force stop. Service is stopped.
It seems that the behaviour of Service and Thread are the same. So, why I should use Service instead of Thread? Is there anything I had missed out? I thought my Service suppose to keep running, even after I kill my app?
Nope. Service will stop running when you kill your application. When you kill your application all components of it are killed (activities, services, etc.).
In general the behaviour of Thread and Service are similar. However, If you start a Thread from an Activity and then shutdown the activity (ie: quit your application), eventually Android will notice that your process has no active components in it (since Android doesn't recognize your Thread as an active component) and it will just kill your process, thereby killing the thread.
However, if you have a Service running, then Android will notice that you have a service running and not kill it so readily. However, it is still possible that Android will kill your service process if it isn't "in use".
Actually there are different kinds of services you can implement. Use a Service instead of IntentService. There you need to look at START_STICKY , START_NOT_STICKY and START_REDELIVER_INTENT you can keep your service running in background even if your activity dies. Android services
You are using startService(). The Service will run until it's code is done, or until Android decides it should be killed. Look up on bound services. On your Activity.onDestroy() you should call unbindService().
In your IntentService you can override onStartCommand() returning START_REDELIVER_INTENT
Then if killed, your service will be restarted automatically by the system after some time with the same Intent.
Be sure to call the super implementation on onStartCommand() like this:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent,flags,startId);
return START_REDELIVER_INTENT;
}
You can invoke setIntentRedelivery(true) in the constructor of the IntentService