Can I detect when my service is killed by "Advanced Task Killer" - android

My app runs a geolocalisation service that the user can active or disactive by a toggleButton. To check the status of the service, I write a boolean in the Shared Preferences. I listen the beginning of the service and the end of it thanks to the onDestroy() of my service.
My problem is that: When the user kill the service with the "advanced task killer", I can't know that the service is killed, the onDestroy is not called !
How can I deal with that?
Thanks for your help.
Florent

When a process is killed (using ATK or android's own force stop button, or the function here), it is immediately purged from memory (if the kernel allows it). This means there's no chance for any additional code to run, meaning there is no way to really deal with a "force kill" sent to your application.
If you want to handle this, you have 2 options (that I can think of):
Publish a disclaimer telling users to add your app to the "ignore" list of ATK.
Find some way to maintain functionality without relying on the onDestroy() method.
EDIT:
If you want to check for your process from a list of currently-running processes, look into getRunningAppProcesses().

onDestroy()
If you want to restart the service, you can restart in onDestroy().

Related

How mark my Android Service to did not stop by user?

I have a service in my android application and I need that it never stop.
How to mark my service so the user could never stop it?
If the user goes to: "Setting - Applications - Running Services - My Service" and click on STOP, my service should not be stopped.
How can I do this?
You cannot prevent stopping a service. The same way you cannot prevent somebody from force closing your app. Even if it were possible, the user could still kill your service by uninstalling your app. Why do you want the service not to be stopped?
On second thought you could have two services that monitor each other periodically by checking ActivityManager.getRunningServices(). When one service is killed the other could restart it. This would probably be considered malicious activity on your part, and you would likely receive very negative reviews.
I'm going to sidestep the obvious question:
Why would you want to do this?
and give you a workable answer. You need two services. Each one monitors the other. If the user kills service A, service B restarts it. If the user kills service B, service A restarts it.
The services will have to be very quick to respond and relaunch the service. This would prevent the user from killing them both at once. So you'll need to design a very quick heartbeat service, but it should be possible. Of course, this will not prevent the user from simply uninstalling your app.
"I fight for the users".
I would be frustrated with an app that didn't let me cancel a service it was running.
I can't think of a use case in which an app (not something in the system) should prevent the user from cancelling something. You should, of course, notify users if the service isn't running, and provide an easy way to restart it.

lifecycle of Service

In my activity I implement onstop(),onstart(),onstart()
I start and bind my service. My service extends IntentService
so onStop() I just call unbindService().
Then I go Settings of my device and open Running tab-> show cached processes
I see that:
when a touch back key. in show cached processes my service has still.
when a touch home key. in show cached processes my service has gone.
Both of them call onstop().
So can anybody can tell me why there have 2 cases like this? Or is this is a misunderstanding about the lifecycle of Service and Activity.
I can't tell you why there are 2 cases like that. However, I can tell you that whether a service is cached or not by the Android OS is not dependent on what button you use to call onStop(). The decision to cache a process is made by the OS depending on how much memory is needed by the service and how often you use it among several other factors.
To quote Google on this matter: " It may cache processes that you’ve been using recently in RAM, so they restart more quickly when you open them again, but it will erase the cache if it needs the RAM for new activities." [1]
[1] - http://support.google.com/mobile/bin/answer.py?hl=en&answer=168921

Android service stopping unexpectedly

I have a service that also contains an activity the problem is sometimes it gets shut down by the operating system.
How can I prevent that from happening?
Not sure what you mean by "service that contains an activity".
The OS can and will shut down services based on demand for memory. How often this happens depends on what you have running on your device, and the amount of memory that your device has.
To try to minimize this, you can call Service.startForeground(). Please read the javadocs for the proper usage, and please look at stopForeground() as well.
Note that this is still no guarantee. You must author your app to gracefully handle when your service is destroyed. Please read up on the Service lifecycle to understand how to handle this.
Even if you could keep your service running at all times, it would be a bad idea. This is not a personal computer plugged in to AC power. A service that is perpetually running will greatly effect the battery life of the device.
It doesn't make any sense that you would have a service that contains an activity. You can have a service to which an Activity is connected, but not one that holds an Activity. Services, along with the rest of your app, can be killed off by the Android OS at any time, and will be restored when the user navigates back to them. Instead you need to design your app in such a way that it is tolerant to these lifecycle events, and doesn't need to be kept around forever.
AFAIK, a service or an activity can not be guaranteed to run all the time. You have to handled the lifecycle events of the service like shutdown and start.

Can a service be killed by a task killer

I'm write an auto-speaker app which has a service inside, and listen to the phone state change.
I expect that the service should be exist all the time and can't be killed.
However, I found that if I use some task killer to kill my app, there will be few seconds my listener won't work.
Although my app seems to be restart automatically in few seconds later and work again.
Why the service could still be killed by task killer??
Is a service just like an activity, just with no UI, and able to restart automatically?
Thanks for your answer.
You cannot exempt your service from being ended by the user or automatically by the OS when resources are needed for that matter.
The best you could do is attempt to write code to compensate for restarts but certainly don't write as if your service will run forever without exception.
EDIT
Android Developer documentation Managing the Lifecycle of a Service has useful information on recovering from stops and when the callback hooks get called and not.
Add this trick to your service onCreate:
startForeground(R.string.app_name, new Notification());
It will prevent service from closing on low memory, task killing etc..
Because taking away the ability of the user to end a process when he/she wants to is just wrong. Instead try to write a work-around so in the event of of the service shutting down, XXXXXX takes place.

How do i know when my app is going to kill

I am working on Gps based application which runs all the time in background. But in extreme low memory case(or when i open many apps) my app is killed by the android system.
I want to relaunch my app when it is killed by android system.I google it for this but don't get any solution.Any help would be appreciated.
Thanks
Have you done your GPS logic inside a service? The service is one of the last things that will be killed and mostly because it has to be killed to free needed memory.
Maybe your service should create a notification so the user is informed and can restart the app by hand by clicking the notification...
Have you looked at this Activity Lifecycle flowchart? In the case where other applications need memory, onPause() is the last guaranteed thing that happens before your app is stopped or killed. So that is where you would need a notification (as #WarrenFaith says) or another mechanism which could potentially revive your app or at least ping the user to revive it.
I think [startForeground()][1] is what you are looking for. Android won't kill your service, so you don't have to worry about restarting it.
Good luck
Tom
[1]: http://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)
You could try launching a new instance of the app when the onDestroy() function is called. For example:
#Override
public void onDestroy() {
Intent intent = new Intent(Thisclass.this, Thisclass.class);
Thisclass.this.startActivity(intent);
super.onDestroy();
}
Maybe you can make use of intents, send a token out to another listener when this application is being killed by android, so that the other serivce can initiate this activity.
Though I cannot see how this will not be a loop. The way you end your app is very similar to the way android ends your app! so I am pretty sure it will be looping infinitely when you try to end the app, behaving very much like a malware!

Categories

Resources