使用 LocationClient 定期获取更新的最省电的方法是什么? [英] What's the most battery-efficient approach of using LocationClient to periodically get updates?

查看:32
本文介绍了使用 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.

在电池寿命方面,如果获取用户的位置将成为应用的主要消耗,我还应该考虑做些什么吗?或者,是否有不同的方法来处理两个警报?我最初只有一个警报,但执行 (!mLlocationClient.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 定期获取更新的最省电的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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