Android: target or application equivalent of onPause

My application uses a remote service to play audio. I do this so that no activity owns the sound - the user can play some audio, which will play from one action, and the sound will continue to play, as they move around the application. However, I want to say a service for pausing or stopping sound playback when the user โ€œunloadsโ€ the application either by backing up, locking the display, or clicking on Home. When the application was one I did it in onPause. So, I think I'm essentially looking for "onPause" at the application / task level. Is there such a thing to exist? If not, what is the best way to get a notification that a task has been suspended by the user, or by supporting all exit or defeat at home?

+4
source share
2 answers

Hmmmm ... unfortunately, I suspect there is no simple answer. I think that why the built-in media player and Pandora use notification to easily allow the user to return to the application to make him shut up.

If the activity flow is fairly linear, then the return from the application is the same as the return to the original activity.

The trick I used in one scenario was to have each action notify the service in onPause() and onResume() . The service will monitor them, and if for some period of time it received onPause() without subsequent onResume() , it was assumed that the user had left (HOME, incoming phone call, incoming text message, answering any application through a notification, etc. d.).

You can also watch ACTION_SCREEN_OFF broadcast Intents to process this script.

I'm sorry that I do not have a better answer for a silver bullet - maybe someone else will have a better idea.

+5
source

I was in a somewhat similar situation, and essentially I did what Mark suggested; Instead of a separate notification, I simply asked the services to count the calls in my registerCallback () and unregisterCallback () APIs, which should have performed all the actions anyway.

+1
source

All Articles