在活动之间进行切换时如何保持GPS开启,而在“居家"活动时如何保持GPS保持关闭状态被按下 [英] How to keep GPS turned on when switching between activities but off when when "home" is pressed

查看:52
本文介绍了在活动之间进行切换时如何保持GPS开启,而在“居家"活动时如何保持GPS保持关闭状态被按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过onResume和onPause来实现将Listener定位的最佳方法.最好我不能在onPause上关闭它并在onResume上重新连接.但是,当我想要的只是在应用程序运行期间使GPS保持开启状态时,我会一直保持断开连接-重新连接.当按下Home键(或其他应用程序正在中断)时,可以关闭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开启,而在“居家"活动时如何保持GPS保持关闭状态被按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆