我们面临的问题是在后台服务中经常从GPS获取位置,但有时会在开放区域自动停止位置,而我确信服务是连续运行的,并且在设备空闲模式下也不会影响深度睡眠情况。请一些身体帮助。
initLocationManager()方法通过后台服务调用一分钟的间隔。
private void initLocationManager() {
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null) {
mLocationManager = (LocationManager)
getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
if (mLocationListeners == null) {
mLocationListeners = new MyLocationListener();
try {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
Constant.LOCATION_INTERVAL,
Constant.LOCATION_DISTANCE, mLocationListeners);
} catch (SecurityException ex) {
mLocationManager = null;
mLocationListeners = null;
WritingFile.getInstance().writeFile(Constant.ERROR_LOG, true,
AppManager.getInstance().getCurrentDateTime() + ex.getMessage() +
"20" + "\n");
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
mLocationManager = null;
mLocationListeners = null;
WritingFile.getInstance().writeFile(Constant.ERROR_LOG, true,
AppManager.getInstance().getCurrentDateTime() + ex.getMessage() +
"21" + "\n");
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
} catch (RuntimeException ex) {
mLocationManager = null;
mLocationListeners = null;
WritingFile.getInstance().writeFile(Constant.ERROR_LOG, true,
AppManager.getInstance().getCurrentDateTime() + ex.getMessage() + "22"
+ "\n");
Log.d(TAG, "System Exception! Thread has no looper." +
ex.getMessage());
} catch (Exception ex) {
mLocationManager = null;
mLocationListeners = null;
WritingFile.getInstance().writeFile(Constant.ERROR_LOG, true,
AppManager.getInstance().getCurrentDateTime() + ex.getMessage() + "23" +
"\n");
} catch (Throwable ex) {
mLocationManager = null;
mLocationListeners = null;
WritingFile.getInstance().writeFile(Constant.ERROR_LOG, true,
AppManager.getInstance().getCurrentDateTime() + ex.getMessage() + "24"
+ "\n");
}
}
}
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location == null) {
//WritingFile.getInstance().writeFile(Constant.COORDINATE, true,
AppManager.getInstance().getCurrentDateTime() + " NULL" + "\n");
return;
}
//lastUpdatedTime =
Constant.SIMPLE_DATE_FORMAT.format(location.getTime());
if (location.getAccuracy() < 50) {
latitude = location.getLatitude();
longitude = location.getLongitude();
// WritingFile.getInstance().writeFile(Constant.COORDINATE, true,
lastUpdatedTime + " "+location.getLatitude() + ","+
location.getLongitude() + "AC"+ "\n");
} else {
if (latitude == 0.0 || longitude == 0.0) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
//WritingFile.getInstance().writeFile(Constant.COORDINATE, true,
lastUpdatedTime + " "+location.getLatitude() + ","+
location.getLongitude() + "NON AC"+ "\n");
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.AVAILABLE:
//WritingFile.getInstance().writeFile(Constant.COORDINATE, true,
AppManager.getInstance().getCurrentDateTime() + " AVAILABLE" + "\n");
//WritingFile.getInstance().writeFile(Constant.COORDINATE, true,
"Last Update Time" + lastUpdatedTime + "\n");
break;
case LocationProvider.OUT_OF_SERVICE:
//WritingFile.getInstance().writeFile(Constant.COORDINATE, true,
AppManager.getInstance().getCurrentDateTime() + " OUT_OF_SERVICE" + "\n");
// WritingFile.getInstance().writeFile(Constant.COORDINATE,
true, "Last Update Time" + lastUpdatedTime + "\n");
removeLocationManager();
mLocationManager = null;
mLocationListeners = null;
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
//WritingFile.getInstance().writeFile(Constant.COORDINATE, true,
AppManager.getInstance().getCurrentDateTime() + "
TEMPORARILY_UNAVAILABLE" + "\n");
//WritingFile.getInstance().writeFile(Constant.COORDINATE, true,
"Last Update Time" + lastUpdatedTime + "\n");
removeLocationManager();
mLocationManager = null;
mLocationListeners = null;
break;
}
}
@Override
public void onProviderEnabled(String provider) {
//WritingFile.getInstance().writeFile(Constant.COORDINATE, true,
AppManager.getInstance().getCurrentDateTime() + " PROVIDER ENABLEE" +
"\n");
}
@Override
public void onProviderDisabled(String provider) {
//WritingFile.getInstance().writeFile(Constant.COORDINATE, true,
AppManager.getInstance().getCurrentDateTime() + " PROVIDER DISABLE" +
"\n");
}
}