我正在尝试在后台服务中运行位置更新。该服务正在运行其自身的工作,已经做了很多其他的事情,比如套接字通信。我希望它也能处理位置更新,但这似乎只适用于某项活动。据我所知,这是由于workerthread上缺少消息循环。我想我需要在某个地方使用Looper.prepare()
,但也许我需要另一个线程来处理位置请求?我似乎无法让模拟器响应任何地理修复事件,所以我一定做错了。
以下是针对所有不相关部分的服务代码。
public class MyService extends Service implements LocationListener {
private Thread runner;
private volatile boolean keepRunning;
private LocationManager locationManager = null;
@Override
public void onCreate() {
keepRunning = true;
runner = new Thread(null, new Runnable() {
public void run() { workerLoop(); }
});
runner.start();
startGps();
}
@Override
public void onDestroy() {
keepRunning = false;
runner.interrupt();
}
private void workerLoop() {
//Looper.myLooper(); How does this work??
//Looper.prepare();
// Main worker loop for the service
while (keepRunning) {
try {
if (commandQueue.notEmpty()) {
executeJob();
} else {
Thread.sleep(1000);
}
} catch (Exception e) {
}
}
stopGps();
}
public void onLocationChanged(Location location) {
// Doing something with the position...
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
private void startGps() {
if (locationManager == null)
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager != null) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(true);
criteria.setCostAllowed(false);
criteria.setSpeedRequired(true);
String provider = locationManager.getBestProvider(criteria, true);
if (provider != null)
locationManager.requestLocationUpdates(provider, 10, 5, (LocationListener) this);
}
}
private void stopGps() {
if (locationManager != null)
locationManager.removeUpdates((LocationListener) this);
locationManager = null;
}
}
答案 0 :(得分:0)
LocationListener不关心它是在活动或服务的上下文中。 你想在workerLoop()中使用uodates的位置,对吧?位置更新(对LocationListener的调用)和workerLoop()都相互独立。要获得workerLoop()的位置更新,您需要加入两个线程。一种方法是使用黑板模式:LocationListener将新位置写入类的字段(这很容易,因为两者都在同一个类的上下文中):
private Location blackboard = null;
public void onLocationChanged(final Location location) {
if( location != null )
this.blackboard = location;
}
private void workerLoop() {
...
if( this.blackboard != null ) {
final Location locationUpdate = this.blackboard;
this.blackboard = null;
// .. do something with the location
}
...
}
使用上面的代码,当workerLoner在读取或删除该位置时,当LocationListener写入黑板时,您可能会遇到竞争条件。这可以通过像这样的同步块来包围对黑板的访问来解决:
synchronized(this) {
this.blackboard = location
}
同样在workerLoop()中,我们必须声明黑板是volatile:
private volatile Location blackboard = null;
或者,您可以使用锁定,授予文档了解更多详情:http://docs.oracle.com/javase/tutorial/essential/concurrency/newlocks.html