IntentService不會等待您的onLocationChangedListener,如果您的IntentService的最後一部分是註銷您的位置更改偵聽器,那麼存在您的問題。
您可以做的是將您的IntentService轉換爲常規服務。檢查不同的操作,其中之一是您收到新位置時的下一步。
即
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
Intent intent = new Intent(this,MyService.class);
intent.setAction("LOCATION_RECEIVED");
intent.putExtra("locationUpdate",location);
locationManager.removeUpdates(this);
this.startService(intent);
}
public void onStatusChanged(String s, int i, Bundle bundle) {}
public void onProviderEnabled(String s) {}
public void onProviderDisabled(String s) {}
}
,並在您服務...
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent!=null) {
String action = intent.getAction();
if (action!= null) {
if (action.equals("LOCATION_RECEIVED")) {
Location location = null;
if (intent.hasExtra("locationUpdate")) {
location = intent.getExtras().getParcelable("locationUpdate");
//Process new location updates here
}
另一種方法是使用掛起的意圖您LocationListener的,但它會爲上面的代碼相同的效果。第三種方法是從OnLocationChangedListener發佈一個可運行的IntentService(我個人還沒有試過這個)。
如果你可以在這裏分享一些代碼,將不勝感激。如果您還有其他問題。當我正在從事類似項目,從服務中獲取位置時,我可能會有所幫助。
你的代碼在哪裏? – GrIsHu