Android位置每分钟更新一次

编程入门 行业动态 更新时间:2024-10-26 06:32:06
本文介绍了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位置每分钟更新一次

本文发布于:2023-06-07 15:29:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/568632.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:每分钟   位置   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!