我有一项服务,它在 onCreateMethod 中请求网络位置更新。我已经使用 LocationListener 来接收新的位置、提供者的状态和提供者中的 statusChange。
当设备处于低功耗状态(手机屏幕关闭)时,我得到了与手机屏幕处于活动状态(屏幕打开)时网络提供商相同的位置修复。
public class LocationRegisterService extends Service implements LocationListener{
private String TAG = LocationRegisterService.class.getName();
private LocationManager lm;
public void onCreate()
{
Log.d(TAG,"Entering LocationRegisterService::onCreate()");
//Register for network location updates
lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Log.i(TAG,"Regsitering for Network Location Updates");
Log.i(TAG,"minTime = 0,minDistance = 0");
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
Log.d(TAG,"Exiting LocationRegisterSerivce::onCreate()");
}
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.d(TAG,"Entering LocationRegisterService::onStartCommand()");
Log.d(TAG,"Exiting LocationRegisterService::onStartCommand()");
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onDestroy()
{
super.onDestroy();
Log.d(TAG,"Entering LocationRegisterService::onDestroy()");
lm.removeUpdates(this);
Log.d(TAG,"Exiting LocationRegisterService::onDestroy()");
}
@Override
public void onLocationChanged(Location location) {
Helpers.logScreenStatus(this);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
double accuracy = location.getAccuracy();
String location_timestamp = formatTime(location.getTime());
String log = latitude + "," + longitude +","+accuracy+","+location_timestamp;
Log.i(TAG,log);
}
@Override
public void onProviderDisabled(String provider) {
String log = "Network Provider Disabled";
}
@Override
public void onProviderEnabled(String provider) {
String log = "Network Provider Enabled";
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
String log = "";
switch(status){
case LocationProvider.AVAILABLE:
log = "Network Available";
break;
case LocationProvider.OUT_OF_SERVICE:
log = "Network Out Of Service";
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
log = "Network Temporarily Unavailable";
break;
}
setServiceWakeUpTime();
stopSelf();
}
private void setServiceWakeUpTime()
{
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this,AlarmReceiver.class);
PendingIntent operation = PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
long wakeAt = System.currentTimeMillis() + (60 * 1000);
am.set(AlarmManager.RTC_WAKEUP, wakeAt , operation);
FileLogger.log(this, "Service will wake up after one minute");
}
private String formatTime(long epoch)
{
Date date = new Date(epoch);
String pattern = "y/M/d H:m:s.S";
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
return dateFormat.format(date);
}
}
我从一项活动开始这项服务,并在关闭设备屏幕的情况下行驶了近 50 公里。我附上旅行的日志。
2013/3/21 0:29:58.743 Entering Broadcast Receiver
2013/3/21 0:29:58.756 Screen is off
2013/3/21 0:29:58.778 Exiting Broadcast Receiver
2013/3/21 0:29:58.783 Registering for network location updates
2013/3/21 0:30:53.571 Screen is off
2013/3/21 0:30:53.587 13.07383585,80.22151335000001,50.0,2013/3/21 0:30:8.551
2013/3/21 0:30:53.593 Screen is off
2013/3/21 0:30:53.595 Network Temporarily Unavailable
2013/3/21 0:30:53.613 Service will wake up after one minute
2013/3/21 0:30:53.619 UnRegistering listener for location updates
2013/3/21 0:31:53.676 Entering Broadcast Receiver
2013/3/21 0:31:53.689 Screen is off
2013/3/21 0:31:53.710 Exiting Broadcast Receiver
2013/3/21 0:31:58.516 Registering for network location updates
2013/3/21 0:32:17.583 Screen is off
2013/3/21 0:32:17.594 13.07383585,80.22151335000001,50.0,2013/3/21 0:32:3.544
相同的日志重复了将近一个小时。此代码在 HTC Explorer 中运行 Android 2.3.3。有人对这种行为有答案吗?