Shutdown events on Android

Is it possible to intercept the event of turning off the phone, or perhaps use a different hack? I always have a service running that is part of a non-market application that collects some analytic data. I would like to inform the analyst about the end of the session and the message, and then resume the system shutdown.

I tried to trigger an event from the onDestroy or onShutdown components, but the phone will turn off without telling the time.

By working around this, making many short sessions based on activity screens, you get information about insensitive analytics that I would like to avoid.

+7
source share
2 answers

Create a broadcast receiver by catching the intent ACTION_SHUTDOWN.

In the onReceive () method, you can accomplish what you are trying to do.

http://developer.android.com/reference/android/content/Intent.html#ACTION_SHUTDOWN

+8
source

You can try to register the Shutwdown hook using the Runitme addShutdownHook (Thread hook) method.

In addition, a method reference indicates:

If runFinalizersOnExit (boolean) is called with a true argument, garbage collection and finalization will succeed after all hooks have either completed or failed. Then the VM shuts down.

So, theoretically, the virtual machine will not be completed until your thread ends.

+3
source

All Articles