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!
Related
I'm trying to implement a service relationship that roughly looks like this:
{ACTIVITY} -> {SERVICE1} -> {SERVICE2}
The ACTIVITY starts SERVICE1 which then starts SERVICE2. It is very very important that SERVICE2 can shut itself down. If SERVICE1 crashes, SERVICE2 should have a chance to shut itself down cleanly. I've already achieved that using a remote process for SERVICE2 so if SERVICE1 crashes it can shut itself down.
The tricky part is if the user does a Force Close through the application manager.
I understand that no solution is 100%. BUT! I've noticed that the Yahoo Weather app is able to run something they're calling a "Watchdog" in a completely separate app line. Killing the main Yahoo weather app doesn't kill the Watchdog app. WHAT VOODOO IS THIS? And how can I replicate something similar?
Images of the yahoo app:
https://dl.dropboxusercontent.com/u/2193687/device-2014-05-22-151216.png
https://dl.dropboxusercontent.com/u/2193687/device-2014-05-22-151236.png
(converting conversation in the comments to an answer)
Background
Typically a "watchdog service" would refer to a service running in a separate process that would try to restart some other target service if it crashes, or if the user force closes it.
Another similar trick would be to register with the AlarmManager to broadcast a periodic intent that would restart your app / service.
Somewhere along the way (HoneyComb I think), Android changed their security model. Apps could be marked as "bad" by the system, which would not launch them again until the user manually launched them. An app became "bad" if it crashed too often, or the user force closed it.
This includes apps that receive the ON_BOOT_COMPLETED intent - they will not restart if they have been marked bad like this.
So the "watchdog" stopped being as useful (some may say annoying, or battery draining) as it used to be. It might still be useful for an app that has an occasional crash
Remote Process
I initially recommended a remote process as the best way to accomplish this. I then saw that you had already done this, and it works.
Unfortunately this will still not be able to restart the service if it has been stopped manually. It doesn't seem like Yahoo's WeatherServiceWatchdog is able to restart the main Weather service either.
I am using a timer in my application for repeated task. By default, the timer should repeat the task with a delay of one second. I am starting the timer when the application Starts. All this is works perfectly. Also,When I came out of my application and open some other application, the timer keeps running.
When I open Google Maps application, my timer stops running. I don't know why this is happening. I googled and found from the activity life cycle that, if other applications needs memory, all processes will be killed. This is what happening in my case, I guess.
I do not want my timer to stop. It must run always till the user uninstall my application.
Why the above problem occurs?
How to achieve my requirement?
Does Using services will solve the problem? If so, Is there any way to keep timer always ON without using services?
Does Alarm Manager be helpful? Even if the user restarts the phone, the timer should work properly.
Yes, a service will solve your problem. The persistence of an Activity is governed by its lifecycle, so in the end, you have no control of its execution. A service is persistent in that it will not be shut down by the system. If you are doing extensive calculation, however, be warned that the OS will not be as generous with resource allocation as it is with an on-screen activity.
In short:
1) Well, from the information you've given, I suppose you drew the right conclusions.
2) Through a service.
3) Yes, it will solve your problem, no, I don't think there is another way to do that, at least not from within an activity.
4) If you're not actually asking about the persistence of a Java.util.Timer but for a way to have a piece of code executed at certain times, this might be the better (/easier) solution. I don't know how well it works for rather frequent timings, though. A service can be resumed on system restart, so no worries about phone shutdown. You can subscribe to the startup event.
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.
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().
Probably it's not a really complicated question, but first of all, I have no idea, what search query should I search for?!
At the beginning of my application I would like to start GPS and if my application will be enden GPS should be closed.
How can I check, if the whole Application (not an Activity) is finished?
Is it enough to use onDestroy-Method for Start-Activity, which will never closed with finish()?
Thank you very much and sorry for a beginner's question.
Mur
UPD
I saw the first answer and I'd like to say once.
I don't mean an ACTIVITY, I mean really the whole APPLICATION (in which many activities exist).
How to check if all application's activities were finished and only in that case stop the service?
Is there posibility for that?
UPD2:
I've tested my solution on a device:
"Is it enough to use onDestroy-Method for Start-Activity, which will never closed with finish()?"
Yes, it was enough.
Make it so that, each Activity binds (via bindService) with the Service...When all the activities have been terminated (unbinds implicitly), your Service will perish. Since the Service will remain alive as long as someone is binding with it.
In this particular case what you need to do is:
Create an Activity to show information to the user.
Create a Service that will run on background and will send the updates to the Activity
There are lot's of examples of what you are trying to do, but basically you can start the Service on the onStart() method of your Activity and ending the service on the onDestroy().