使用 LocationClient 定期获取更新最省电的方法是什

What#39;s the most battery-efficient approach of using LocationClient to periodically get updates?(使用 LocationClient 定期获取更新最省电的方法是什么?)
本文介绍了使用 LocationClient 定期获取更新最省电的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑设置两个单独的警报来每小时收集用户的位置数据,一个每 59 分钟触发一次以连接"客户端,另一个用于实际获取位置,然后断开客户端.

I am thinking about having two separate alarms to gather a user's location data every hour, one that goes off every 59 minutes to "connect" the client and a second to actually get the location and then subsequently disconnect the client.

在电池寿命方面,如果获取用户的位置将成为应用程序的主要消耗,我还应该考虑做些什么?或者,是否有不同的方法来设置两个警报?我最初只有一个警报,但执行 (!mLocationClient.isConnected) 然后进行连接检查并没有给客户端足够的时间来连接.

In terms of battery life, is there anything else I should consider doing if getting the user's location will be the primary drain of the app? Or, is there a different approach to having two alarms? I originally only had a single alarm, but performing a (!mLocationClient.isConnected) then connect check does not give the client enough time to connect.

感谢您的洞察力.

两个警报会像这样响起:

The two alarms would go off something like this:

private int PERIODIC_UPDATE = 60000*60;  //gets location and disconnects every hour
private int PERIODIC_RECONNECTION_UPDATE = 60000*59;  //connects 1 minute before getLocation call

    Timer toReconnect = new Timer();
    toReconnect.schedule(new TimerTask() {

        @Override
        public void run() {
            mLocationClient.connect();
        }
    }, 5000, PERIODIC_RECONNECTION_UPDATE);

    Timer theTimer = new Timer(); 
    theTimer.schedule(new TimerTask(){
        @Override
        public void run() {
            try {
                if(!mLocationClient.isConnected()) {
                    mLocationClient.connect();
                    //This will not have much affect because cannot so quickly, will remove. 
                }

                Location theLocation = mLocationClient.getLastLocation();
                if(theLocation!=null) {
                    checkPostLocation(theLocation); 

                    mLocationClient.disconnect();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }   
        }}, 5000, PERIODIC_UPDATE);

推荐答案

你真的需要跟踪用户吗?

Do you actually need to track the user?

如果只是关于 UI,则使用 getLastKnownLocation(PASSIVE_PROVIDER),假设他们在其他地方使用手机上的定位服务,您应该会得到一些半准确的信息.

If it's just about UI, then use getLastKnownLocation(PASSIVE_PROVIDER) and you should get something semi-accurate assuming they used location services on their phone somewhere else.

如果您需要实际对用户进行三角测量,请实现不同的供应商使用不同的电池.被动<网络<全球定位系统.

If you need to actually triangulate the user, realize the different providers use different battery. Passive < Network < GPS.

定位用户越多,GPS 消耗的电量和时间就越多.

The more you locate the user, the more battery with GPS taking the most battery and time.

按计划启动服务,1 小时或其他任何时间,只需要一项服务.最多只能活 1 分钟(或更短),收听所有位置提供商.在分钟或准确度足够好后,您保存结果并关闭服务.

Start the service by intent one a schedule, 1 hour or whatever, only one service necessary. Only live for a maximum of 1 minute (or less), listen on all Location providers. After the minute or accuracy is good enough, you save the result and shut down the service.

这篇关于使用 LocationClient 定期获取更新最省电的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)