在我的代碼中,我使用的是IntentService
來偵聽位置更新(GPS或網絡更新),並且在收到事件時觸發此IntentService
,因此它將從startService()
開始從任何活動開始。IntentService:如何正確排隊?
public class AddLocationService extends IntentService implements LocationListener {
/*My code here*/
}
@Override
protected void onHandleIntent(Intent intent) {
if(getOldLoc() == null)
{
//Get a new location
this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_INTERVAL_GPS, 0, this);
this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_INTERVAL_GPS, 0, this);
Log.d(AddLocationService.TAG, "Network listener started");
this.time_start_listening = System.currentTimeMillis();
mTimerThread mTimerRunnable = new mTimerThread();
this.timerThread = new Thread(mTimerRunnable);
this.timerThread.start();
}
else
/*REUSE OLD LOCATION*/
}
現在我的問題是:當兩個事件開始這個IntentService
和第二啓動它,而第一個是仍然要求更新,我會想第二個要等到第一種是完全完成(位置找到OR定時器線程完成)。 但是,無論何時第二次執行IntentService
(第一個實例仍在運行),它都會打印日誌並按照並行執行的方式執行。
不過我認爲的IntentService
的主要目標是,它是什麼順序所以第二意圖將不得不等待,直到第一個做...
難道我missunderstood的東西嗎?
嗨@Rob,謝謝你的回覆。 當你說'onHandleIntent'方法沒有阻塞線程時,你可以更具體嗎?我應該怎麼做才能阻止它? 從另一個活動創建意圖時,它與Flags有關嗎?還是我應該在'onHandleIntent()'方法中做的事情? – Pom12
編輯:我想我明白你的意思:我的IntentService返回時仍然請求更新。 我怎樣才能阻止它。這個二人組「wait() - notify()」是個好主意嗎? – Pom12
類型,我更新了我的答案,並提供了更多關於如何修復它的信息 – Rob