我开发了一个Android应用程序,每隔10秒使用GPS获取当前位置,并使用套接字将其发送到服务器。为了实现这一点,我使用postDelayed方法来获取当前位置并将其发送到服务器。
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;
}
但是此代码会导致设备过热,并快速消耗电池,是否有另一种方法可以达到相同的效果并减少过热?
由于
答案 0 :(得分:0)
首先,不要使用getLastKnownLocation。它不仅通常会返回null,循环调用它是一种极其低效的处理方式。相反,requestLocationUpdates,它会在有新位置时给你打电话。
其次,不要每隔10秒将该位置发送到服务器。发送它经常导致您的手机不断地打开收音机(无线电或手机),这会导致您的发热问题。发送数据会产生热量。每分钟左右减少一次,并向该位置发送航向和速度数据。如果您需要更精确的位置,服务器可以使用速度和航向以编程方式计算可能的位置 - 如果需要。很可能你甚至不需要它。