Stopping and starting a Service based on application state

后端 未结 8 1459
北海茫月
北海茫月 2021-01-05 16:45

I have a Service which tracks the location of the user. Currently, the Service boots when the application starts and stops when the application terminates. Unfortunately,

相关标签:
8条回答
  • 2021-01-05 16:58

    I have not tried this approach but I think you can override the home key of android device by using KeyEvent.KEYCODE_HOME and you can use stopService(Intent) to stop your service and when again application resumes, you can write startService(Intent) in the onResume() method of your Activity.

    This way I think your service will only stop when user explicitly presses home button to take application in the background and not when he switches from one activity to another.

    0 讨论(0)
  • 2021-01-05 17:02

    Reading all the above answers I would suggest Simply add a boolean global flag for each activity & put it in your onResume & onPause & also while launching an Activity Something like this

    public void onPause()
    {
    super.onPause();
    activity1IsResumed = true;
    }
    

    &same for onResume

    & similarly when launching a new Activity

    startActivityForResult(myintent ,0);
    activity2IsResumed = true;
    activity1IsResumed = false;
    

    then in your Service simply check

    if(activity1IsResumed || activity2IsResumed || activity3IsResumed)
    {
    //your logic
    }
    else
    {
    //another logic
    //or dont run location tracker
    }
    

    & you are done!

    0 讨论(0)
  • 2021-01-05 17:03

    Create methods registerActivity() and unRegisterActivity() in your Application object and implement first method in all you acts onResume() and second in acts onPause().

    First method add activity to List<Activity> instance in your app object, unRegisterActivity() checks size of list in every call if==0 stopService();.

    0 讨论(0)
  • 2021-01-05 17:13

    What I would suggest is overriding the onPause/onReume methods as others have said. Without knowing more about the flow of your application and interactions between Activities, I can't give much more information beyond guesswork.

    If your Activities are persistent, however, my recommendation would be to utilize the Intents better when switching between Activities.

    For instance, each Activity should have a boolean "transition" flag. So, when you move from one Activity to the next, you set up an Intent extra:

    intent.putExtra("transition",true);
    

    Followed in the receiving Activity by: (in onCreate)

    intent.getBooleanExtra("transition",false);
    

    This way, for each Activity that launches, you can know whether it has come from another Activity, or if it has been launched from a home screen launcher. Thus, if it gets a true transition, then onPause should NOT stop the service--that means you will be returning to the previous Activity after it returns. If it receives no "transition" extra, or a false transition, then you can safely assume there is no Activity underneath it waiting to take over for the current one.

    On the first Activity, you will simply need to stop the service if you are switching to another Activity, which you should be able to figure out programmatically if one Activity is started from another.

    0 讨论(0)
  • 2021-01-05 17:16

    You should override the onPause and onResume methods on your Activity. If you have multiple activities you may want to have a common base class for them and put the start/stop logic into the base class.

    0 讨论(0)
  • 2021-01-05 17:19

    I haven't tested this yet, but it looks like if you use Context#bindService() (instead of Context#startService()), the service should stop when no more activities are bound to it. (see Service lifecycle).

    Then use onPause()/onResume() in each activity to bind/unbind from the service.

    Alternatively, you could add a pair of methods on your service which tell it to start/stop listening for location updates and call it from each activity's onResume()/onPause(). The service would still be running, but the location updates wouldn't be draining the battery.

    0 讨论(0)
提交回复
热议问题