stop app, including all activities and services - android lifecycle - android

I would like to simply stop the app, and all its activities and services. Currently, from my main activity (which had started other activities), I call finish(), and the app appears to stop, as it goes to the home screen of my device. However, when I check running apps on the device, this app is still listed. It says "one process, and one service". Is there a way to just kill everything? Or, if I have to do it individually how would I find what is running, and how do I stop it? thanks

Is there some specific reason you want to make sure the process is killed?
Android manages processes intelligently. It keeps your process around, and if the user starts your app again it can use the existing process, rather than start up a new one. And if your device start running low on memory, Android will start killing off these inactive processes to free up resources.
In short, it's a good thing that Android keeps your process around. You shouldn't want to have it killed needlessly.
Although it sounds like you may not be stopping your application's service. If you use bindService to start the service from your activity, the service will automatically be stopped when the Activity is stopped (assuming nothing else has bound to the service). Alternately, if you use startService to start the service, you need to call stopService to stop it.

Related

How to keep android process alive

How does the apps like Swiftkey, Locker Master manages to keep process alive even after it has been removed from back stack?
They also don't use sticky notification.
Update:-
I need to keep my process alive. But in my case service is active but process gets killed.
They all have one or more unbound background service.
To kill those services you can go into Settings -> Apps -> Running.
However the services may be restarted at any time by other apps or system by sending a system wide message (intent). In most cases it is restarted by
Time events
Boot complete
Other apps
Other intents
In the Swiftkey case, it will be started by Android OS when it needs to show a keyboard.
EDIT: you can specify that a service runs in a remote process by adding this to the service definition in AndroidManifest
android:process="process_name_here"
However there is no such thing as a service that cannot be killed and can run forever. Android OS may start killing your service if it is running low on resources or the service is running for a long period of time. To overcome this, you can make a forground running service, but it needs to show a notification. It can also be killed by task managers, like you mentioned. You should instead start focusing on how to save it's state so you are able to restart it later.

Android:nonstop Back ground service

I have started a service from my application and from that service a worker thread is started .I want my service to run even application goes background and until the user kills/exits the application.
But some cases my service got killed due to low memory ,then used sticky service or making the app to foreground to restart the service.
My issue is I dont want to lose the data between service ending and restarting time ,so is it possible to start another thread from service ondestroy method, but in this case how we can control that thread.
Please let me know is it the right approach ,and is this usecase achievable
I want my service to run even application goes background and until the user kills/exits the application.
This is not possible. The user can always get rid of your app, via Force Close in Settings, or via some device's version of the recent-tasks list.
But some cases my service got killed due to low memory
No, your process is terminated for low memory.
My issue is I dont want to lose the data between service ending and restarting time ,so is it possible to start another thread from service ondestroy method
No, because your process is being terminated.
Please let me know is it the right approach
Probably not. Very few apps need a service that runs constantly, which is why Android, and its users, go to great lengths to control such services. I would recommend that you try to find some solution to whatever your problem is that does not need a service running constantly.

Keeping a service running in all activities of an app

How do I keep a service running, when all activities of an application are viewable. and close the service only when I leave the app?
Is there a way to start a service in one activity of an app and stop that same service in another activity of that same application?
I don`t want that service to continue running when the user leaves the application.
There is really no concept of 'application' and leaving it. What happens if open a link from one of your activities, launch the browser to view it, then come back via the back button? Did you really leave the app?
What does your service do? Do activities bind to it? If so, it will be automatically shut down after the last client unbinds. If not, it should shut itself when it has finished doing it's work (Cf. IntentService). If it doesn't fit either of those patterns, maybe you don't need a service at all, just some background thread(s)?
Edit (based on comments below):
For a service running a media player, the usual way is to have an ongoing notification for the service that lets the user bring up an activity to control the service. Or have buttons on the notification in JB to achieve something similar. Additionally, if you make the service a foreground one, that will give it higher priority and it is less likely to be killed if resources are low.

Force closing an app kills the service of another app with the same shared user id?

I have 2 apps with the same SharedUserId value in the manifest, App1 and App2. App1 has a Service component which is specified to run in a separate process (in the manifest android:process=":remote"). The main purpose of this service is to check on App2. I would like the service to do some cleanup when App2 closes.
When I force close App2, App1's service is killed, and its onDestroy() method is not called.
How can I ensure that App1's service is not killed (or at least finishes with calling onDestroy) when I force stop App2?
edit: Android 4.0.1 on Galaxy Nexus if that makes a difference
Yes that is possible. If you are using sharedUserId imagine that those 2 applications run in the same thread which it means they have the same application life cycle which basically means that with the "force close" on any of these applications you terminate their thread.
Meaning that everything on this thread is killed. One of the negative things in using sharedUserID and sharedProcess between applications rather than using content providers.
I don't think there is a work around when you are force closing the app. Maybe you should just finish it the "normal" way and let the os kill it.
UPDATE from your comment:
In general I don't tend to intervene with these kind of stuff. The user might want to manually close the app, as I am used to doing, and it would be irritating if you have a service or the app running again and again.
That's my point of view. Despite my belief, there is a trick to manually relaunch your application after force closing when an unhandled exception had fired and I guess the reaction of the force close by user would be the same. You could add the pending intent in the service I guess: Android: How to auto-restart application after it's been "force closed"?

Android: Creating a service that survives the application that started it

I'm developing an Android application that consists of:
a lightweight background service that logs events to a DB
a heavier GUI application that summarizes these events and displays graphs.
I'm having trouble creating the service part, though. The graphic application can use quite some RAM, and when it goes to the background, the OS closes it after some time of not being used.
The problem is, when the application gets shut down, so does the service. This is bad because this keeps me from recording further events. I don't care if the application gets terminated, but the service needs to keep on running.
I have tried numerous ways to keep the service alive, like having it use threads or a differently named process than the main app. Nothing has worked, and I have found no help on any of the android developer pages or forums.
Thank you for your advice!
Try to return START_STICKY in your service's onStartCommand(). Also how do you start your service? If you use bindService() with BIND_AUTO_CREATE flag it will be stopped automatically on unbindService(). You should explicitly start it with startService() and stop calling stopSelf(). Than OS keep your process running on the background after activity will be closed. Note: the activity and the service run in one process and it's imposible: "application gets terminated, but the service needs to keep on running". But it is possible to keep the process running without activities, with service running on the background.
Read the detailed info http://developer.android.com/reference/android/app/Service.html.
good luck!
You should use the AlarmManager to respawn your service. You just can't keep your service alive eternally.
Check about the lifecycle : http://developer.android.com/guide/topics/fundamentals.html

Categories

Resources