I have an application, where I have put a connection to e certain service in a custom Application class, so that all activities can access it, but I need to close the service once my application is closed.
I want to send the bundled data just one time, not every time a single activity hits onPause/Destroy().
I also figured onTerminate() in the Application class is not always invoked.
Is there a better way to do this?
If you are targeting API level 14 or above you can use the registerActivityLifecycleCallbacks to register a listener and count the launches/stops in onActivityResumed and onActivityStopped. When the counter is 0 then you know there is no more activity in the stack and you can close your service
unfortunately, there is no callback / method to detect when your application process has stopped, or going to be stopped. #Alex suggestion to listen to the Activity lifecycle callbacks can be more tricky then it seems.
but if you want to detect this only for purposes of stopping running Service , then this problem is not relevant, because if your process stooped, then all running services it owns is also stopped anyway!!
if you meant that you want to stop the service when the application is not in foreground (completely different thing..) then the solutions is different:
if you'll always use bindService() instead of startService() within the onResume activity callback, and in the onPause() stop it, then when your app would enter background - the service will be stopped automatically.
another option would be to use the Service callbacks to detect if currently there is bounded activity:
#Override
public IBinder onBind(Intent intent) {
mBoundedToActivity = true;
return mBinder;
}
#Override
public void onRebind(Intent intent) {
mBoundedToActivity = true;
super.onRebind(intent);
}
#Override
public boolean onUnbind(Intent intent) {
mBoundedToActivity = false;
return super.onUnbind(intent);
}
Related
I am trying to understand the Service Life Cycle while working through some Android Open Source Code.
I was looking at a Service implementation which I distilled down to something like the following...
public class MyService extends Service {
public MyService() { super(); }
#Override
public void onCreate() {
super.onCreate();
init();
//==this seems odd to me
//comment in AOSP says startService() is called to make
//sure Service stays around long enough for the async call
//to complete.
startService(new Intent(this, myservice.class()));
doSomeMoreInitAsync();
}
#Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
if(actionableIntent(intent,flags,startId)) {
//do something
//NOTE: the Intent passed to startService() in onCreate()
//above will go around this block of code, doing nothing
//except returning START_STICKY
}
return START_STICKY;
}
public void onDestroy() {
//destroy stuff
}
#Override
public IBinder onBind(final Intent intent) {
return mBinder; //an instance of android.os.Binder derivative
// created when this service was instantiated
}
//other stuff
}
Why would someone want to have onCreate() call startService() on itself like above, doing nothing? The comment in code sheds some light, but it's like assumptions are being made about the Life Cycle that I don't understand. I.e., is it reasonable to expect onCreate() to effectively start its own service?
I know that if a service has already been started then onCreate() will only be called once (unless destroyed and restarted, then a new instance of the service is created and onCreate() is called once on it). My first concern with this example would be that there is an expectation placed upon the underlying Service API implementation that the Service is already in the initialized state before onCreate() is called (else there be an infinite recursion, but there is not).
But isn't onCreate() supposed to be part of the initialization (albeit an optional part for the subclass)?
Is this coding logic a reasonable way of making sure the Service is forced to be an Unbounded Service? Or am I looking at a bad example in the AOSP which may have undefined behavior in the future?
You are correct in that a Service will call onCreate and onStartCommand if it is started via Context.startService. So in this sense, when you return START_STICKY, the Service will continually run until an explicit call to stopService() is called. It will also be destroyed and restarted during this lifecycle.
Another way to create a Service, is by binding to it. As per the docs:
Clients can also use Context.bindService() to obtain a persistent connection to a service. This likewise creates the service if it is not already running (calling onCreate() while doing so), but does not call onStartCommand().
So, it's possible for a Service to be created by simply binding to it. However, the lifecycle of a Service indicates that it will remain if it is started or a client is still bound to it. Meaning, that if it was created by a bind command, it will immediately be destroyed as soon as the client unbinds.
So, if a Service starts itself in the onCreate(), it will ensure that it puts itself in the started state regardless of whether it was created by binding or by an explicit call to startService. Since there's no actionable intent, the onStartCommand will just pass straight through. An clients that call startSevice will, presumably, have actionable Intents in which case the Service will perform its duties.
I'm reading the official documentation about Services, but I'm a little bit confused because some things aren't so explicit.
Let's take this snippet:
public class MyService extends Service {
public int onStartCommand(Intent intent, int flags, int startID) {
while(...any condition...) {
//some actions
}
return START_SOMETHING;
}
public IBinder onBind(Intent intent) {
return null;
}
}
My doubts are the followings:
The Android system can destroy a service not bound to the current focused activity and not declared as in foreground. But can the Android system destroy a service even if it is still in the while loop?
When I call stopService() does it wait for the onStartCommand() to get to the return statement?
Where should I insert the stopSelf() call? Just before the return statement?
Can the Android system destroy a service even if it is still in the while loop?
Yes. For some reason, extreme low memory pressure (for example), Android system kills the Service without calling onDestroy() method. Why? Because of Service always runs on UI thread.
When I call stopService() does it wait for the onStartCommand() to get to the return statement?
No. Return statement's action will be executed when Android system kills the Service. But, if you call stopSelf() manually, the return statement's action will be ignored.
Where should I insert the stopSelf() call? Just before the return statement?
Call stopSelf() wherever and whenever you want. For example, if user press a button, call stopSelf() to stop the Service playing a music.
I want to perform some operation when my application gets killed.
Which method can be used for this? I am working on Android 5.0.
The key of this question is that:
you must understand your application whether can receive any
additional callbacks when your application being killed in any kinds of situation.
The following answer is answered by Devunwired in this question:
Android app doens't call "onDestroy()" when killed (ICS)
This will help you more to understand this.
Your application will not receive any additional callbacks if the process it terminated by external means (i.e. killed for memory reasons or the user Force Stops the application). You will have to make do with the callbacks you received when you app went into the background for your application cleanup.
finish() is only called by the system when the user presses the BACK button from your Activity, although it is often called directly by applications to leave an Activity and return to the previous one. This is not technically a lifecycle callback.
onDestroy() only gets called on an Activity as a result of a call to finish(), so mainly only when the user hits the BACK button. When the user hits the HOME button, the foreground Activity only goes through onPause() and onStop().
This means that Android doesn't provide much feedback to an Activity to differentiate a user going Home versus moving to another Activity (from your app or any other); the Activity itself simply knows it's no longer in the foreground. An Android application is more a loose collection of Activities than it is a tightly integrated singular concept (like you may be used to on other platforms) so there are no real system callbacks to know when your application as a whole has been brought forward or moved backward.
Ultimately, I would urge you to reconsider your application architecture if it relies on the knowledge of whether ANY Activity in your application is in the foreground, but depending on your needs, there may be other ways more friendly to the framework to accomplish this. One option is to implement a bound Service inside of your application that every Activity binds to while active (i.e. between onStart() and onStop()). What this provides you is the ability to leverage the fact that a bound Service only lives as long as clients are bound to it, so you can monitor the onCreate() and onDestroy() methods of the Service to know when the current foreground task is not part of your application.
You might also find this article written by Dianne Hackborn to be interesting covering in more detail the Android architecture and how Google thinks it ought to be used.
You have to use Service Class for it like -
public class Myservice extends Service { #Nullable #Override public IBinder onBind(Intent intent) { return null; }
#Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(Constants.TAG, "Service Started"); return START_NOT_STICKY; }
#Override public void onDestroy() { super.onDestroy(); Log.d(Constants.TAG, "Service Destroyed"); }
#Override public void onTaskRemoved(Intent rootIntent) { Log.e(Constants.TAG, "END"); //Perfome here want you want to do when app gets kill stopSelf(); } }
In Manifest -
<service android:name="Myservice"
android:stopWithTask="false" />
In Oncreate of your launcher activity or Application Class to start service -
startService(new Intent(getBaseContext(), OnClearFromRecentService.class));
You can use your activity's onDestroy() method.
My app has a background Service. In the Service I need to detect and log all app launches. For example: whether the user opens Facebook or Google+ or Twitter (any app) - I want a receiver in my Service to catch it for me to perform an action.
The only way I have been able to come up to do this - is to have a Timer running in the onCreate() function of my Service. My concern is that this 1 second timer may drain battery.
Is that assumption correct?
If yes, is there another way (some intent filter?) that I can register with my Broadcast Receiver to catch any App Launch?
Things I have tried:
(1) My Service code with the TimerTask. My trigger action code will be placed inside the "run()" function of the TimerTask.
public class KillService extends Service {
ActivityManager mActivityManager;
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onDestroy() {
unregisterReceiver(receiver);
super.onDestroy();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
#Override
public void onCreate() {
super.onCreate();
Timer timer = new Timer();
TimerTask refresher = new TimerTask() {
public void run() {
//PLACING MY TRIGGER ACTION HERE
};
};
//TIMER RUNS EVERY 1 SECOND
timer.scheduleAtFixedRate(refresher, 1000,1000);
}
}
(2) Reviewed Available Broadcast Actions
I reviewed the broadcast_actions.txt that comes in the SDK Folder (sdk\platforms\android-19\data), but I did not find any Intent that will be appropriate for this use case.
Link to file
All I want is to know when any App Activity is started (i.e. in the Foreground) so that I dont have to continually check with Timer (afraid that it may drain battery)
Perhaps my answer in this question might help you. Android how to know an app has been started and range apps priority according the starting times
Place that code inside your background service.
Based on the package name of the activity in the foreground, you can detect that app name by checking all the apps on the phone and matching it with the app that has the same package name.
Edit
Since you want a continuous check, you could use an AsyncTask(), and in the doInBackground() function, just keep getting the list of running activities in each iteration, like I have suggested in that link, and the first item in the taskInfo list will have the activity/app in the foreground. You can also provide another button in your app which on click you stop this , by keeping a bool variable in shared preferences (which you poll for in the while condition in doInBackGround() function of the AsyncTask()).
I have done something similar in the past, and it worked for me.
I created a service that can be started via the startService() method, but it also be bound by the applications. I wish that it can be started only via the startService() method, or in other words, applications should be able to bind to it only when the service is already started.
The service should be started manually and not when an application binds to it.
If the service is running, applications can bind to it.
If the service is not running, when an application tries to bind to it, the service should not start.
However, the default behavior is quite different: in fact, Android starts the service automatically when an application wants to bind to it. I would like to know if is possible to modify this behavior to achieve the above requirement.
If this is not possible, the only alternative would be to stop the service if it has been initiated as a result of a bindService(). Here are the changes to my service class in order to use this way...
// It says if the service was started manually.
private boolean mCorrectlyStarted = false;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(LOG_TAG, "onStartCommand()");
mCorrectlyStarted = true; // the service is started manually: ok!
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
Log.i(LOG_TAG, String.format("onBind(intent: %s)", intent));
if (mCorrectlyStarted) {
return binder;
}
else {
stopSelf(); // although I make this call, the service is not stopped
return null;
}
}
Why do my changes do not have the desired effect?
It might be good that the service starts when an application invokes the bindService() method, but after verifying that it was not started manually, it should stop itself.
The fact that something, like an Activity, called bindService() means someone is still bound to the service. Calling stopSelf() does not destroy the service since technically the service is still bound by the activity.
It will be destroyed once unbindService is called eventually by the Activity. Binding and starting a service are orthogonal.
I am not sure what you are trying to achieve with this design, maybe provide more info on why you want the service to be started before something can bind to it?