本文介绍了如何保持GPS开启活动之间的切换时的不过关的时候,当"家"是pressed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出实现监听器与onResume和位置的onPause最好的方式。最好我能做到不是将其关闭在的onPause并重新连接上onResume。但后来我一直有断开重新连接时,我要的是为GPS停留在应用程序的时间。如果家是pssed $ P $(或其他应用程序中断),那么GPS可以击落关闭节省电池。

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.

任何想法?

感谢。

推荐答案

您的问题可以概括为我怎么知道什么时候我的应用程序移动到/出前景的?我曾在一个需要辨别这种能力的两种不同的应用程序中使用下面的方法成功。

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();
    }
}

这篇关于如何保持GPS开启活动之间的切换时的不过关的时候,当"家"是pressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:14