简体   繁体   中英

How can my service be notified when the Home or Back buttons are pressed

I would like to have my service perform something when the user presses Home or Back. I'm not looking to override or block the normal behavior of these buttons, just get notified about them.

I need to get notify even when the activity that started the service is stopped.

Working with platform 17-19.

Is there any way? Can it be done by intent-filter or broadcast receivers??

Thanks

Only Activities (and Fragments) will get these callbacks (onPause, onStop etc.). If your Activity is running, it can notify the service, but once it's in the background there is no way to do this.

If the service is on you can use a broadcast by hooking into onPause or on onStop methods in the activity. if the service is not on I think you have to start it, do something, then end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM