Press home button to stop service

后端 未结 3 387
谎友^
谎友^ 2021-01-16 12:08

i want to if the user press home button my service will be stop , this is my code but it\'s not working for me, please help me:

@Override
public boolean onKe         


        
3条回答
  •  天涯浪人
    2021-01-16 12:46

    If you're trying to make sure the service only runs when the related activity is visible, a better solution would be to stop the service in the onPause() method of whatever activity the user is running. That way, the event is handled when the user presses "back" and when the user presses "home".

    Likewise, you can bind your service to the activity (or activities). The bound service will be destroyed when all activities bound to it stop.

提交回复
热议问题