Android位置每分钟更新一次 [英] Android location update every minute

查看:165
本文介绍了Android位置每分钟更新一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在编写一个应用程序,该应用程序每分钟(或其他指定的时间)接收位置数据,然后将其发送到服务器.

I am currently writing an application that receives location data every minute (or other specified amount of time) and thes send it to server.

我注意到的问题是: 当我将应用程序连接到电源时,它可以正常工作-以定义的时间间隔发送更新. 当我在电池上使用它时,它会随机发送更新.

The problem that I have noticed is: When I use my application connected to power source it works perfectly - sends updates with defined interval. When I use it on battery it sends updates randomly.

对于我的应用程序,我使用计时器指定更新间隔.此计时器在后台服务中运行.我要求在计时器中进行更新,并在收到位置信息时删除更新. 我的代码片段:

For my application I use timer to specify interval of update. This timer runs in Background service. I request updates in timer and remove updates when I receive location. Fragment of my code:

    private class mainTask extends TimerTask
    { 
        public void run() 
        {
            toastHandler.sendEmptyMessage(0); 
        }
    } 

    private final Handler toastHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            date = date.getInstance(); //used to set time of update.

            if(!GPSupdating)
            {
                locationManager.requestLocationUpdates("gps", 0, 0, locationListenerGPS);
                GPSupdating = true;
            }
        }
    };

我的位置侦听器:

    LocationListener locationListenerGPS = new LocationListener() 
    {

        public void onLocationChanged(Location updatedLocation) 
        {
            myGPSLocation = updatedLocation;
            haveLocationGPS = true;
            locationManager.removeUpdates(locationListenerGPS);
            GPSupdating = false;
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {}

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {}
    };  

您知道为什么它不能正常工作吗?

Do you have any idea why this is not working correctly?

推荐答案

您不会通过每分钟启用和禁用GPS来节省能源.选择更大的间隔(5分钟),或者第二次获取该位置.
然后将位置存储在lastValidLocation字段中,启动自己的计时器,并每分钟读取一次lastValidLocation.并发送到服务器(如果已更改).

You will not save energy by enabling and disabling GPS once per minute. Either choose a greater intervall (5 minute) or get the location evry second.
Then store the location in a lastValidLocation filed, start your own Timer, and once a minute read out lastValidLocation. and send to server if changed.

这篇关于Android位置每分钟更新一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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