我想在每两分钟后通过GPS在Android(5.1.1)中获取位置。当屏幕打开时应用程序正常工作但是当屏幕关闭时它无法通过GPS获取位置。谁能建议我做什么? 以下是我的代码。
startLoggingService() {
if (locManager == null) {
locManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
}
final Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(true);
criteria.setSpeedRequired(true);
criteria.setBearingRequired(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
String bestprovider = locManager.getBestProvider(criteria, true);
if (bestprovider != null && bestprovider.length() > 0) {
locManager.requestLocationUpdates(bestprovider, gpsMinTime,
gpsMinDistance, locationListener);
}
}
private final LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
didFindLocation = true;
latitude = location.getLatitude();
longitude = location.getLongitude();
altitude = location.getAltitude();
speed = (float) (location.getSpeed() * 3.6);
dt = location.getTime();
gpslatitude = String.valueOf(latitude);
gpslongitude = String.valueOf(longitude);
gpsaltitude = String.valueOf(altitude);
gpsspeed = String.valueOf(speed);
SimpleDateFormat sdfDateTime = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
datetime = sdfDateTime.format(new Date(dt));
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
};
答案 0 :(得分:0)
因为你的设备在屏幕关闭时进入睡眠状态......
你应该使用部分唤醒锁....有关详细信息,请参阅Simplest way to detect a mobile device问题