views:

64

answers:

2

Hello,

Is there a way to notify an activity/service of a force-close request right before it gets killed?

I mean when the user hits the force close button in Menu>Settings>Applications>Manage applications>app name>Force Close.

Thanks!

A: 

I'm pretty new here and to Android programming in general, so forgive me if this isn't strictly correct. However, my understanding is that the Application Not Responding (ANR) dialog is only displayed if your application is either a) non-responsive or b) has thrown an exception that was not caught.

In case a) the application will not be able to respond to any force-close notification in a reasonable amount of time since it's already hung up enough to generate the ANR. In case b) your application is already dead.

commie64
thanks commie64, but thats not the case that i meant...just like the task manager can kill running processes in windows, you can also kill running applications in android regardless of their actual running state (frozen or running)
Shatazone
I'm sorry, in my exuberance to answer I misunderstood your question. I'm not sure what the etiquette is here, should I delete my answer? Also, I'm guessing this method you're referring to basically simulates what the ANR is doing. Since there'd be no way for the ANR to notify a frozen/crashed app, I don't believe it calls any cleanup methods prior to killing it off. So I would guess neither does this method. Again though, that's just a guess, sorry.
commie64
+1  A: 

I think the ActivityManager just kills the hosting process, so you may not be able to get any event/message/warning. To check you could create an app that has a single Activity that lets you know if onDestroy is called, and further if isFinishing is invoked.

The path to "Menu>Settings>Applications>Manage applications>app name>Force Close" in the source, in case it helps, is:

ManageApplications: http://android.git.kernel.org/?p=platform/packages/apps/Settings.git;a=blob_plain;f=src/com/android/settings/ManageApplications.java;hb=master

InstalledAppDetails: http://android.git.kernel.org/?p=platform/packages/apps/Settings.git;a=blob_plain;f=src/com/android/settings/InstalledAppDetails.java;hb=master

ActivityManager.forceStopPackage: http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob_plain;f=core/java/android/app/ActivityManager.java;hb=HEAD

ActivityManagerNative.forceStopPackage: http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob_plain;f=core/java/android/app/ActivityManagerNative.java;hb=HEAD

Once it gets the native level it hands off to a Service that does the dirty work. Again, I would guess it just stops the process, but that's a guess, and I don't know off the top of my head where the other side of the native interface is in the source to check (though it's there somewhere if you poke around and want to know for sure).

Also you might want to add why you want this information to your question, there might be a different/better angle to getting what you want accomplished, depending on that.

Charlie Collins
thanks alot man for this massive amount of information!what im trying to do is to launch a "app B" once "app A" is force closed, do u think this could be achieved in android? :S
Shatazone
@Shatazone: I sincerely hope what you want is impossible. Malware would use that technique in a heartbeat.
CommonsWare
I was guessing that you might want to just log the force closes, for feedback or such. In terms of launching another app, I agree with CommonsWare that it's definitely not a good idea. If the user is force closing your app, they want it to close, and they don't want that action to result in another app launching.
Charlie Collins
hmmm ... yeah it might be used as a security gap if it is possible to be handled...
Shatazone