【发布时间】:2014-04-14 14:01:15
【问题描述】:
对,情况如下;
我们开发了一款应用,它每 5 到 10 分钟检查一次用户的位置,以查找特定位置的内容。为此,我创建了一个服务来固定在后台(因此即使应用程序不直接在前台也可以更新),其中创建了一个 LocationController 来检查最新的已知位置。完成后,它会进行清理并将位置发送到数据库(这是此概念的必要条件)。
只要我使用 GPS_PROVIDER 检查位置,这一切都很好。但是,当我将它切换到 NETWORK_PROVIDER 时,它可能会在完全死亡之前再次检查位置。
我已尝试多种选项来防止此问题,但当我交换该 1 设置时,更新服务似乎没有任何效果。
这里有一些sn-ps应该与服务相关:
更新服务:
@Override
public void onCreate() {
super.onCreate();
Log.d("Debug", "Created service");
PowerManager mgr = (PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
wakeLock.acquire();
IntentFilter filter = new IntentFilter();
filter.addAction(UPDATE_ACTION);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("Starting", "Starting Service");
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(UPDATE_ACTION);
alarmManagerPendingIntent = PendingIntent.getBroadcast(this, 0, i, 0);
alarmManagerPendingIntent.cancel();
mAlarmManager.cancel(alarmManagerPendingIntent);
mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
AlarmManager.INTERVAL_FIFTEEN_MINUTES,
AlarmManager.INTERVAL_FIFTEEN_MINUTES,
alarmManagerPendingIntent);
mLocationController = new LocationController(this, this);
updateHandler = new Handler();
return START_STICKY;
}
位置控制器:
private LocationListener locationListener = new LocationListener() {
private boolean didSendLocation;
public void onLocationChanged(Location location) {
mLastLocation = location;
mCallback.locationUpdate(location,false);
didSendLocation = true;
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Debug", "Status changed: "+status);
Location _lastLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.d("Debug", "Last location: "+_lastLocation);
if(!didSendLocation)
{
mCallback.locationUpdate(_lastLocation,false);
}
didSendLocation = false;
}
public void onProviderEnabled(String provider) {
Log.d("Debug", "Provider Enabled");
}
public void onProviderDisabled(String provider) {
Log.d("Debug", "Provider disabled");
}
};
public LocationController(Context mContext, LocationControllerCallback callback) {
this.mContext = mContext;
mCallback = callback;
Log.d("Debug", "Created LocationController");
mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300000, 100, locationListener);
mLastLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
mCallback.locationUpdate(mLastLocation,true);
}
所以,代码就像现在一样工作,但是当我交换时:
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300000, 100, locationListener);
与
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 300000, 100, locationListener);
它将不再更新。有什么想法吗?
【问题讨论】:
标签: android service location locationmanager