I tried closing the application using the finis开发者_如何学运维h(), but finish() method just finishes the activity, the application does not closes. Now I am using System.exit(0) to close the application sometimes it works but sometime it throws an exception. How can I completely exit from an application??
Okay, you can do onething get ProcessID of your application and in onDestroy() kill that process. Thats it
int pid=android.os.Process.myPid();
android.os.Process.killProcess(pid);
Android's memory model is a bit weird - basically, the OS will decide for you when it wants to actually exit. As long as your app is not using bandwidth or excessive CPU time (read: the user's money or CPU time), the Android system's design is such that you're supposed to just leave it running, and the system will kill the app itself when it needs the resources.
What you need to do is stop using (through finish() or other means) bandwidth or CPU time or the like, reset any state you need in order to provide your desired user experience, drop any wake locks or similar miscellaneous hardware modifications you've made, and end any services you really don't want to keep running with stopSelf().
If/when the OS runs out of memory (which is the only time when it really matters whether an activity is actually gone or just not doing anything at the moment) it will unceremoniously force kill activities or services (without running any cleanup code or event handlers) based on a fairly complex set of priorities that basically boils down to "don't throw out anything that the user is using/will notice."
精彩评论