How to keep GPS turned on when switching between activities but off when when “home” is pressed

你。 提交于 2019-12-21 06:18:16

问题


I am trying to figure out the best way to implement Listener to location with the onResume and onPause. Best I can do not it to turn it off on onPause and reconnect on onResume. But then I keep having disconnect-reconnect when all I want is for the GPS to stay on for the duration of the application. When Home is pressed (or another application is interrupting) then GPS can be downed off for battery saving.

Any ideas?

Thanks.


回答1:


Your question can be generalized to "How do I tell when my app moves into/out of the foreground?" I have used the following approach successfully in two different apps that needed the ability to discern this.

When you change activities, you should see the following sequence of lifecycle events:

Activity A onPause()
Activity B onCreate()
Activity B onStart()
Activity B onResume()
Activity A onStop()

As long as both of these activities are yours, you can make a singleton class designed to track whether your app is the foreground app or not.

public class ActivityTracker {

    private static ActivityTracker instance = new ActivityTracker();
    private boolean resumed;
    private boolean inForeground;

    private ActivityTracker() { /*no instantiation*/ }

    public static ActivityTracker getInstance() {
        return instance;
    }

    public void onActivityStarted() {
        if (!inForeground) {
            /* 
             * Started activities should be visible (though not always interact-able),
             * so you should be in the foreground here.
             *
             * Register your location listener here. 
             */
            inForeground = true;
        }
    }

    public void onActivityResumed() {
        resumed = true;
    }

    public void onActivityPaused() {
        resumed = false;
    }

    public void onActivityStopped() {
        if (!resumed) {
            /* If another one of your activities had taken the foreground, it would
             * have tripped this flag in onActivityResumed(). Since that is not the
             * case, your app is in the background.
             *
             * Unregister your location listener here.
             */
            inForeground = false;
        }
    }
}

Now make a base activity that interacts with this tracker. If all of your activities extend this base activity, your tracker will be able to tell you when you move to the foreground or the background.

public class BaseActivity extends Activity {
    private ActivityTracker activityTracker;

    public void onCreate(Bundle saved) {
        super.onCreate(saved);
        /* ... */
        activityTracker = ActivityTracker.getInstance();
    }

    public void onStart() {
        super.onStart();
        activityTracker.onActivityStarted();
    }

    public void onResume() {
        super.onResume();
        activityTracker.onActivityResumed();
    }

    public void onPause() {
        super.onPause();
        activityTracker.onActivityPaused();
    }

    public void onStop() {
        super.onStop();
        activityTracker.onActivityStopped();
    }
}


来源:https://stackoverflow.com/questions/15995311/how-to-keep-gps-turned-on-when-switching-between-activities-but-off-when-when-h

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!