I created background service on android tab using startService() method inside the Activity.
Then I create a Thread on onStart() method. I read from the android developer site that service are independent of the activity, but when I destroy the Activity android system kills the service too. I want the service to continue until I do not stop it myself.
I am aware that it can be done using AlarmManger but it consumes battery. There is no need of push notification, but the service needs to run in the background. Any suggestions on how to solve this?
Use this in your service class and call it by using button
#Override
public void onDestroy() {
Log.e("serive destroyed", "service distroyed");
super.onDestroy();
}
check this link for AsyncTask won't stop even when the activity has destroyed & in that check the Answer Posted By Snicolas.And take Reference of other Answers. Hope this helps.
It should not generally happens when activity destroys service also destroys.
Services are components that run in the background, without a user interface.
More important, Android services can have life cycles separate from activities. When an activity pauses, stops, or gets destroyed, there may be some processing that you want to continue. Services are good for that too.
Related
Is it possible to identify who destroyed the service ?
I have a service that is running in the background and when the user clicks a button i call
stopService(myService):
and
onDestroy()
is then called . Sometimes the service is destroyed by the operating system for one reason or another . is it possible to find out if the user stopped it or was it the system ?
I have a service that read accelerometer data and based on it an action is taking .
the service i running fine but some times the system terminates this service and when that happens i want to service to restart self using AlarmManager after a set seconds of time.
I want to check in onDestroy() if the user is the one who stopped the service thats by callking stopService() or the system did . if the user did then there is no need to restart the service but if it was the system then restart it
You haven't provided enough information to be sure but you're most likely looking at the background limitations of Android Oreo. To increase battery life all background services will be stopped by the system a few minutes after the app moves to the background.
More information about the background limitations can be found here.
If your Android version is lower than Oreo or your app is still in the foreground you'll need to provide more information about the circumstances.
#HemantParmar gave a good link to read, it is simple to check if user destroyed the service or system itself, whenever you call stopService() use a boolean flag.
//runningService.stopit();
// OR
stopService(RunningService); // user called
boolean iStopped = true;
public void stopit(){
//stopSelf();
}
public void onDestroy(){
super.onDestroy();
if(iStopped){
// user did it
}else{
// system did it
}
iStopped = false; // whoever did it , reset it
}
stopService() & stopSelf() are called only when you want to explicitly destroy service which in turn call onDestroy().
And onDestroy() will also be called when you service finishes itself.
Please refer for more details:
Android: stopService() doesn't call onDestroy()!
https://developer.android.com/guide/components/services
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.
Firstly, sorry for my English if it's not enough good. I'm having some problems in my application.
Starting, my app has multiple activities and one service which works in background since the first activity execute it. If I press back button on my root activity, I exit from the app but the service continue working. Then, I go into the app back, and the service work perfectly. My problem comes when I press a button to exit the application (there, I stop service and finish the root activity mainly) and then exit without any problem, when I want to enter the app again, the service is started, but if I want to change to another activity (which doesn't have the serviceConnection) my service get called onDestroy() method without any reason for that. I don't have how to continue, because the usual way to execute in this case is the service go on working as the first case.
Thanks a lot.
There is for sure a reason why onDestroy gets called.
In the first section of 'Services' in the developer guide, you can read the following:
Multiple components can bind to the service at once, but when all of
them unbind, the service is destroyed. (link)
So, if all components unbind from the service, the service will get destroyed. When you enter the activity that is not bound to the service, the service will be destroyed.
I'm wondering why you don't want your service to be destroyed since you don't need it in your 'another' activity?
I have an Activity that starts a service. When the activity is closed, I want the service to continue running in the background. I have a couple of questions here.
Will closing the activity screen cause the activity to actually stop? Or do I need to forcibly stop it to cause it to stop?
If closing the screen does cause it to stop, then I assume I need to use startService to start it. Is that correct? If that is the case, is there a way to get a handle to the running service next time the activity starts? If it is not the case, then I can just bind to the service.
When your Activity is no longer visible on the screen, it is stopped. Stopping an Activity bound to a Service does not stop the Service. However, you'll want to make sure you unbind from the Service when your Activity calls onDestroy() to make sure you don't have any dangling handlers and suchlike.
You can rebind to a running Service the same way as you did the first time. Sending an Intent to start a Service that's already running doesn't create a second instance of it, so that's safe.
A good way to start a Service like you're describing is to start it using the Context.BIND_AUTO_CREATE argument to your call to bindService().
The details of all of this can be found at the Android docs about bound services. It can be a little confusing at first. Follow the tutorial code closely since it sounds like your problem maps well onto the sample they provide.
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.