安卓跟踪应用程序导致设备过热



我开发了一个安卓应用程序,它每10秒使用GPS获取当前位置,并使用套接字将其发送到服务器。为了实现这一点,我使用了postDelay方法来保持获取当前位置并将其发送到服务器。

myRunnable = new Runnable()    {
@Override
public void run() {
Location mCurrentLocation =getCurrentLocation();
if(mCurrentLocation != null)
sendCurrentLocationToServer(mCurrentLocation);
Handler.postDelayed(this, 10000);
}};
public Location getCurrentLocation(){
Location currentLocation = myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
return currentLocation;
}

但是这段代码使设备过热,并迅速消耗电池,有没有另一种方法可以达到相同的结果并减少过热?

谢谢

首先,不要使用 getLastKnownLocation。 它不仅通常会返回 null,循环调用它是一种效率极低的做事方式。 相反,requestLocationUpdates,当它有一个新的位置时,它会打电话给你。

其次,不要每 10 秒将位置发送到服务器。 发送它经常导致您的手机保持无线电(wifi或蜂窝(持续打开,这会导致您的热量问题。 发送数据会产生热量。 将其减少到每分钟左右一次,并发送带有位置的航向和速度数据。 如果您需要更精确的位置,服务器可以使用速度和航向以编程方式计算可能的位置 - 如果需要。 很可能你甚至不需要它。

最新更新