As the title, I need that an Android Service - that periodically calls a web service - does the job only when the app is closed. When the app is running (don't matter if in background or foreground) the Service must be stopped.
How I can achieve this?
Thank you.
If you have just one Activity as an entry point for your app it would be very easy. This Activity will be the destroyed at end when user leave your app, so overriding onDestroy() and launching Service from there will do the job. When the same Activity starts you can request stopping your Service inside onCreate() of the Activity.
Related
So I'm creating app that includes ForegoundService with is sending some data to the backend.
I've already made service part. In activity i have two buttons, like start and stop.
Pushing start is starting the service and pushing stop needs to stop him. The problem is that when I reopen the app (from icon, or from notification) the service reference is null and I can't stop service working.
Is it possible to reopen that exact activity event when app was already closed?
I was thinking about making service singleton, but i wish there is some proper way to do this.
When an Android app is forced closed (System.exit(0)) or it crashes, the service associated with it, running in the background is not destroyed. In simple words, the onDestroy() method of service is not called. Because of this when the android app is restarted, the service starts up again ( its onCreate() is called right away). I want to avoid this.
So, the question is :
How Can I Destroy the background service in such a case as I don't have access to its onDestroy()?
If you are extending a regular plain Service class, then what you say won't happen. I think you are starting a thread for your service. If that's the case, then make sure you call stopService with the right intent to close the running Service. I am assuming your Service is a started service type and not binding type.
My application consists of one activity which creates a service. I want the service to be keep running as long as application is running. I know:
It is not guaranteed as Android system can kill activity in low memory conditions and if activity is in background.
The service can be stopped (and killed) by system.
If I bind service to the activity, the activity would get notification in case service is being stopped or started. However, the service may stop running if activity goes in background (onStop()). Please correct me if I am wrong here.
If I bind to service in onResume() of activity and unbind() in onStop(), it might happen that service stops running when my application goes in background. If I bind in onCreate() and unbind() in onDestroy() of activity, would it mean that my activity will keep getting notification from service even when in background.
What is the best way to keep service running and get notification from service to Activity as long as application is running. Please note that there is just one activity in the application so sending activity in background means application goes in background.
Thanks
true
true, but its more rare if us use startForeground()
The service usually won't stop until all activities have unbound. But when the last has, it will. So u can prevent the service from dieing when going to background, if you only unbind in onPause if isFinishing() == true.
see 3.
I personally like to set up a Handler in the Activity and send Messages to it from the service.
If you are binding a Service to your Activity. It simply means that you need service to run as long as your activity is running. If you do not need to bind Service with activity or you do not need to update your UI while your Service is running. you must not bind your Service to your Activity. In this case, for different actions done by Service you can notify user using Android Notifications. Like notifying user that xx download has been completed.
It totally depends upon your purpose that you want to achieve from Service.
if you can use IntentService for your application, you can pass data to the service through an Intent. results can be passed back to the Activity through a ResultReceiver
If you bind your Service to your unique Activity, you'll have it alive as long as the Activity is not terminated or the service isn't unbound. Just bind it on the onCreate() and let it get unbound when stopping your activity (no need to do anything).
You can create a Listener interface within your service, that you'll implement in the Activity, so you can send those notifications from the Service to the Activity. You'll find suitable example and information about this if Googling.
I created an Activity and started a service in that activity.The service should run in the background even if we make the application to force close.Please help me to solve this problem.
Services run on the same proccess of its application. For this, they should die if its application is force closed.
Maybe you should think about using different threads:
Processes and Threads
When app gets force close then service gets destroyed.
Check out below link, which shows a Return value from onstartCommand func.
http://developer.android.com/reference/android/app/Service.html#START_STICKY
By this system restarts service.
Check this also.
http://developer.android.com/reference/android/app/Service.html#START_CONTINUATION_MASK
http://developer.android.com/reference/android/app/Service.html#START_REDELIVER_INTENT
My android application starts a service in the onCreate() callback of a class that extends Application. The service performs some background tasks that are relevant to the user only while the application is running. For that reason I would like to close the service when the application's last activity is closed. I've tried to perform closing the service in the callback onTerminate() , but it never gets called . So what would be the best place where a service should be closed ?
Thanks !
An Android service, once started, will continue running until the Context.stopService() or stopSelf() is called.
There are various hooks you can use to stop the service using Context.stopService (the service itself, or an onDestroy()/onPause callback in one of the activities, or a button click).
It's true that Android does some resource management itself, but it can take a long time before Android decides to terminate your services. And a service that's running but not doing anything just consumes resources on the phone that other apps might need.
In your case, the onPause method of your last activity would be a good that will get called, and as such is the correct place to stop the service.
The onPause() callback will be made when your activity is paused for any reason, and you know that when this happens your app will not be visible again until onResume() is called. If your service has a reason to run in the use case that your activity might be started again soon, you should add an entry to your service that onPause() calls, to set a delayed service termination. In onResume() you can cancel that delayed termination through another entry.