I have added below code in onStartCommand of service-
Notification notification = new NotificationCompat.Builder(AbcService.this)
.setContentTitle("xyz")
.setTicker("xyz")
.setContentText("xyz")
.setSmallIcon(R.drawable.mevo_chat_mevopic)
.setOngoing(true).build();
startForeground(AppConstants.ABC,
notification);
and stopForeground(true); inside onDestroy() of service.I expect service to run even when app is destroyed as it is foreground service but i see that sometimes it runs and sometimes it gets stopped.I don't have any other apps open and my device doesn't have memory issue.What could be possible reasons for my foreground service being stopped and how can i resolve this?
If your Service is started by your app then actually your service is running on main process. so when app is killed service will also be stopped. So what you can do is, send broadcast from onTaskRemoved method of your service as follows:
Intent intent = new Intent("com.android.ServiceStopped");
sendBroadcast(intent);
and have an broadcast receiver which will again start a service. I have tried it. service restarts from all type of kills.
Related
I have an app that uses a ForegroundService and a BroadcastReceiver.
When the app is started, the ForegroundService is started too and the mandatory notification is sent to user to notify that such a service is running. Based on the App's way to work, Service needs to run indefinetely but i would like that the user could eventually shutdown the Service using a TapAction on this notification.
Actually i achieved this result sending an explicit Intent to BroadcastReceiver and then on the onReceive() method send another explicit Intent to the ForegroundService to shut it down with stopService() method.
I was wondering if there is a way to send directly an Intent from TapAction to ForegroundService considering that there is no onReceive() method on Service
I have a foreground service in which I register for location updates, and intent filter for the battery intent Intent.ACTION_BATTERY_CHANGED, and a LocalBroadcast. All of these I remove in the onDestroy of my service as once this service is dead.
I also have changed the return integer in the onStartCommand from START_STICKY to START_NOT_STICKY but this has no effect.
I log when this service is started and destroyed as well as my main activity and it seems that this service just starts on its own without any activities.
The issue is it's a foreground service so a notification accompanies it. This means that when it's started up again randomly, the user sees the notification and I don't want the service to be running on its own accord.
So to recap.
- It is a bound, foreground service.
- I return START_NOT_STICKY in onStartCommand
- When I no longer need it and close the app, I unbind from it, call stopService and the service's onDestroy is called which is where we unregister all of the receivers.
- The service is randomly started after this.
Since you use a foreground service, you should call stopForeground method
"Caution: The integer ID you give to startForeground() must not be 0."
There are only two ways to start a service, either binding or start it from a context, you must make sure you aren't doing it inside your code.
Maybe you should check on the service lifecycle:
http://developer.android.com/images/service_lifecycle.png
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()
I have intent service in my app. This service has some job to do - uploading some files. It runs only when there is something to do.
Service won't upload when some conditions are met, for example no Internet connection. For that reason it registers itself to on broadcast receiver to receive message about Internet connection changes.
The problem is that service is killed with app even if it is doing something, for example:
App is sending intent to service
Service started uploading something, everything fine
X% uploaded, app is killed, service is killed
Internet connection changed - service is woken up.
If service is woken up after app is killed, why is it killed with the app? Is there any way to prevent killing service when app is killed?
I'm killing app manually. I know android could kill my service anytime and I don't want to prevent it. I just want to have this service running after user closed or killed app.
"It runs only when there is something to do." only theoretically :) - maybe that is you what you want to achieve.
"The problem is that service is killed with app even if it is doing something, for example:"
Of course, there will be cases when the user action will end your Service or Intent service.
This is a fail answer.
"Is there any way to prevent killing service when app is killed?"
It is just watch for "parental control" task protection" keywords in Google!
Because you used an intentService that mean the intentService will destroy once the activty destroy
so you have to use Service instead of intentService, so you can uplaod your file in the backgroud.
According to manipuation between the Service and the activty via broadcast receiver or to bind the service to activty.
Edit :
The Service may be triggered from any thread.
The IntentService must be triggered from Main Thread.
If you don't mind showing notification (in your case, you can for example show notification with upload progress), then in your IntentService (or Service) you can call:
startForeground(int id, Notification notification)
This should prevent killing your service when your application is killed.
From documentation: "You can set this flag if killing your service would be disruptive to the user, such as if your service is performing background music playback, so the user would notice if their music stopped playing."
I have a service that runs in the background.
I want this service to see which app the user has in the foreground.
I can do it with the service is running every second and checks which app is in the foreground
this will kill the battery.
Are there an handler or a broadcast that can start the service when the app is changed in the foreground?
I can see that the activitymanager writes to the logcat(in eclipse) when I change apps.