我们的一位程序员开发了一款需要一些地理标记功能的应用程序:
问题是这些设备显示出巨大的电池消耗,在2小时内完全耗尽它们。
代码显示一个服务,每10秒检查一次当前位置,我想这可能是问题,因为它没有进行 ad hoc 检查:
public class BatchService extends Service {
private long UPDATE_INTERVAL = 10 * 1000; /* 10 secs */
private long FASTEST_INTERVAL = 2000; /* 2 sec */
public String latitudine;
public String longitudine;
public int count;
PowerManager pm;
PowerManager.WakeLock wl;
private static BatchService istanza;
public static BatchService getInstance()
{
if (istanza == null)
{
istanza = new BatchService();
}
return istanza;
}
Handler handler = new Handler();
private Runnable periodicUpdate = new Runnable() {
@Override
public void run() {
handler.postDelayed(periodicUpdate, 10000);
count = 0;
startLocationUpdates();
}
};
private boolean isRunning = false;
@Override
public void onCreate() {
Log.i(TAG, "Service onCreate");
isRunning = true;
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "GpsTrackerWakelock");
wl.acquire();
}
protected void startLocationUpdates() {
// Create the location request to start receiving updates
mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
// Create LocationSettingsRequest object using location request
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(mLocationRequest);
LocationSettingsRequest locationSettingsRequest = builder.build();
// Check whether location settings are satisfied
// https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient
SettingsClient settingsClient = LocationServices.getSettingsClient(this);
settingsClient.checkLocationSettings(locationSettingsRequest);
// new Google API SDK v11 uses getFusedLocationProviderClient(this)
getFusedLocationProviderClient(this).requestLocationUpdates(mLocationRequest, new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if(count==0){
count++;
onLocationChanged(locationResult.getLastLocation());
}
}
},
null);
public void onLocationChanged(Location location) {
if(location!=null){
// New location has now been determined
if(location.getAccuracy()<=50){
getInstance().latitudine = String.valueOf(location.getLatitude());
getInstance().longitudine = String.valueOf(location.getLongitude());
}else{
getInstance().latitudine = null;
getInstance().longitudine = null;
}
}
}
}
我们可以调查什么来解决我们的问题并更好地处理这类案件?
由于