目前,我正在嘗試開發一個Android應用程序。應用從GPS
第一(直通onLocationUpdates(GPS_PROVIDER, 0, 0 , Listener)
讀取位置數據,如果GPS
無法檢索我的情況下,5秒)在特定的時間數據(時,又問到Network provider
(直通onLocationUpdates(NETWORK_PROVIDER,0,0, Listener)
)。如何在Android多線程程序中停止looper.loop()?
在我的節目我已經處理。如下這個任務
mWorker = new WorkerThread();
mWorker.start(); // Thread creation and Calling run() Method
class WorkerThread extends Thread{
public void run() {
try {
if(isGPSEnabled){
//Call GPS_PROVIDER for Location updates,
//Will get location value in Location Change Listener
requestLocationUpdates(GPS_PROVIDER,0,0,Listener);
Log.d(TAG,"GPS Listener is registered and thread is going for Sleep");
Thread.sleep(THREAD_SLEEP_TIME);
}else if (isNetworkEnbles){
// if GPS_PROVIDER is not available, will call to NETWORK_PROVIDER
// Will get location value to OnLocationChangeListener
requestLocationUpdates(NETWORK_PROVIDER,0,0,listener);
Log.d(TAG,"NETWORK Listener is registered and thread is going for Sleep");
Thread.sleep(THREAD_SLEEP_TIME);
}
} catch (InterruptedException e){
// Thread is interrupted on OnLocationChange Listener
// Thread has been Interrupted, It means.. data is available thru Listener.
}
// Thread had sleep for THREAD_SLEEP_TIME , but Still data is not available
If (GPS or Network data isnt available thru GPS listener or Network Listener) {
// Do try for one more time.
}
}
現在我OnLocationChanged監聽器的代碼如下:
public void onLocationChanged(Location location) {
// Listener got location
// First Intrupt the sleeping Thread
mWorker.interrupt();
if (loc != null)
{
//get Location
}
}
現在我的問題是,當我運行這段代碼時,Worker Thread從不會調用OnLocationChanged
Listener。
我在工作線程中插入looper.Loop()
,調用了OnLocationChanged
。但它連續運行,並且不會停止。我應該如何停止looper.loop()
?
是否有任何其他方式通過,我可以管理我的上述任務,除了workerThread.
如果你需要進一步澄清,只是讓我知道。
你爲什麼要在單獨的線程中運行這個? – getekha 2011-01-26 15:03:44