I have a service which is successfully started on boot complete using a BroadcastReceiver. But when I am doing a force stop it is not restarting.
I tried starting the service from activity, and tried force stop, but in this case service restarted.
I am using Service.START_STICKY
In other question on stackoverflow, it is mentioned to user BaseContext not ApplicationContext. How do I get BaseContext from a BroadcaseReceiver
START_STICKY restart service, when OS kill it.
If you kill the app, it's not work.
Related
I have coded a simple app in Android Studio. What it does is not important but it starts a Service with Context.startService(Intent i). Till now everything is allright BUT when I kill all tasks with my Task Manager of my phone the Service is killed, too and with it the notification ist creates! I don't understand why.
By the way: I used return START_STICKY at the end of my Service. And it has been started/sheduled by the Timer class with Timer.scheduleAtFixedRate(TimerTask task, int delay, int period). The Service has been started by my Main Application or by the BroadcastReceiver which received BOOT_COMPLETED. The timer has also not been canceled.
Hope you will pardon my english.
Services that are started with startService does not stop until an explicit call - stopService is done on that particular service. Another case when the service stops is when the phone needs more memory its stops the background services and applications. In your case, you are doing the exact same thing. Clicking on the kill all tasks clears the memory which is the exact thing that happens when phone is out of memory and wants more memory. In that case - services are stopped and sticky services are restarted. Now, how to handle the restart of the service properly, kindly search clearing memory stops services android and you will be shown a bunch of answers on stackoverflow !
I have created a simple service and calling it from an activity with startService(intent). I am returning START_STICKY from my service's onStartCommand(). I want the service to keep running even if the activity is closed or removed from task list. When I test on emulator it is working fine - service is restarted after a second if I remove the application from recent screen list (by swiping or clear all). But on real device (running on Android 5.1) the service is killed but not started. I can see the following message.
04-22 20:26:26.436 714-1454/? W/ActivityManager: Scheduling restart of crashed service com.example.xxx.MyPkg/.MyService in 1000ms
04-22 20:26:26.441 714-1454/? I/ActivityManager: Force stopping service ServiceRecord{9df8b23 u0 com.example.xxx.MyPkg/.MyService}
Also, at some other point I have scheduled a start of the same service using Alarmmanager and PendingIntent. If the application is there in recent list, then only the service start works through Alarm, else not. Again, on emulator it is working fine.
I read fews questions and answers which says that START_STICKY has some issues in Kitkat and JellyBean. There were suggestions of scheduling a restart after few seconds from service's onTaskRemoved(). But firstly, I don't see onTaskRemoved() getting called on all clearing actions. And secondly, given that scheduled service start is not working when app is not present in task list, I am not sure it will work even if I schedule from onTaskRemoved() or activity's onDestroy().
Is there any way to get around this issue?
I have a Service running in the foreground, and an Activity that interacts with it. If the Activity crashes, Android kills the entire process, including the foreground Service and its associated Threads.
However, the ongoing notification provided by the Service does not go away, and upon closer inspection, Android's task manager reveals that the Service itself is still running.
How can I kill the foreground Service in this circumstance?
Have you override onStartCommand method of the Service? What value is it returning? If not, try to override it and return START_NOT_STICKY from it.
START_STICKY: If this service's process is killed while it is started, then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service.
START_NOT_STICKY: If this service's process is killed while it is started, and there are no new start intents to deliver to it, then take the service out of the started state and don't recreate.
Not sure, as I have never worked on foreground services, but this might be the reason.
Are you sure the service is not running it its own process...
Also can you confirm whether the service is getting restarted..If its getting restarted-it is because you are returning START_STICKY from onStartCommand()
my question is simply about how do we restart an android service that runs in background in it's own thread from an activity if the service stops itself after completing certain tasks.
Simply call startService() from your activity:
http://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)
Make sure you are extending Service, not IntentService. And then start service in STICKY mode. It should remain active that way. Even if it dies for some reason (system may kill it etc.), it should restart on its own.
Read this link: START_STICKY and START_NOT_STICKY
Given a service that return START_STICKY from its onStartCommand:
1) The documentation mentions killing of the service, as far as I understand this, that is not done by calling Content.stopService, but done by the system if it needs resources (or my service could also crash which would be effectively the same - no clean shutdown) ?
2) How can I simulate the killing mentioned in 1) ? The killing is probably different from stopping (stoppping != killing ?) the service using the application menu in the settings, so I can't use this menu for testing purposes?
3) If my service is killed and then restarted, according to the documentation, only onStartCommand seems to be called, but not onCreate. So I conclude that variables of my service are persistet before my service gets killed? Or why does onCreate not get aclled? Or does it maybe?
My aim is to make sure that my service still works reliable when it was killed and restarted. Thanks for any hint :-)
Killing means its process being killed by the out of memory killer. You can simulate this from the adb shell -- use "ps" to find your app's process, and "kill" with the found pid to kill it. The system will after a few seconds restart the process and service.
If you are only receiving onStartCommand(), then your service and process was already running and didn't need to be re-created.
I had the same issue, and was wondering what is going on.
After reading the referenced post, I simply moved my code from onStartCommand(..) to onCreate(..) and it was working as expected.