2012-10-24 24 views
1

我運行一個類LokasyonBulucu()如果值爲null等待運行功能的Android

LokasyonBulucu lokasyonBulucu= new LokasyonBulucu(); 
lokasyonBulucu.LokasyonBul(context); 

我需要兩個變量從該類緯度和經度

lat=lokasyonBulucu.location.getLatitude(); 
lon= lokasyonBulucu.location.getLongitude(); 

但我要等待類找到座標...因爲需要花費時間。 如果發現緯度和經度我想運行此功能

new arkaPlanIsleri(kategori_id, lat , lon).execute(); 

回答

2

使用for循環來檢查每秒有幾秒鐘等待的最大數量。每秒檢查一次,如果已經找到latlon。如果這樣執行該方法並跳出等待循環:

for(int i = 0; i < 10; i++) { //maximum 10 seconds to wait 
    if(lat != null && lon != null) { //check if the lat and lon are already found 
     new arkaPlanIsleri(kategori_id, lat , lon).execute(); 
     break; //stop the waiting loop 
    } 
    SystemClock.sleep(1000); //wait one second 
}