2012-03-17 50 views
0

我有一個問題:如何暫停某個活動直到某個變量被更改?暫停直到變量發生變化

我已經寫了2班,一個是LocationListener的,一個是創建這個LocationListener的類的實例:

第一類:

LocationGetter locgetter = new LocationGetter(); 
LocationManager locmanag = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
locmanag.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locgetter); 
mLat = locgetter.getLat(); 
mLong = locgetter.getLon(); 

的LocationListener的類:

public class LocationGetter implements LocationListener { 

double mLat = 0.0; 
double mLong = 0.0; 

@Override 
public void onLocationChanged(Location location) { 
if(location != null){ 
    mLat = location.getLatitude(); 
    mLong = location.getLongitude(); 
} 

現在,我希望第一個類中的代碼暫停,直到getLatitude和getLongitude getters實際上返回的值不是0爲止。有一個原因,我沒有想要在獲取經度和緯度之前繼續,如果此功能有效,我會顯示一個進度條。我知道onLocationChanged方法只會在有位置時運行,但第一個類將繼續執行。

我對Android真的很陌生,所以如果我忽略了一些很明顯的東西,請原諒我。

+0

我不認爲「停頓」是一個正確的詞在這種情況下使用...是的,你可以使用ProgressBar,這是什麼錯? – 2012-03-17 15:20:32

回答

0

我會建議使用某種形式的同步,如java.util.concurrent.CyclicBarrier。在Thread-1中調用CyclicBarrier#await(),然後它將等待,直到您還在Thread-2中調用它

2

那麼你可以做

while (value == 0.0) { 
    // do nothing 
} 
// now use value 

但是這是一個非常糟糕的主意。你會得到「應用程序沒有響應」。另外 你不能暫停你的活動,永遠不會 - 而且沒有必要。

你應該做的是添加一種方法來通知你的代碼,一旦它們改變就想使用這些值。 LocationListeners最簡單的方法是直接在Activity中實現它們。像這樣:

class MyActivity extends Activity implements LocationListener { 
    onCreate() { 
     locmanag.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
    } 

    public void onLocationChanged(Location location) { 
     // now change the ui. This is called on UI thread. 
    } 
} 

如果你想要的位置要由不同的類,你仍然可以通知有關更改您的活動來處理。

如果你想用你的LocationListener的距離只是一個活動,將工作:

class MyActivity extends Activity { 
    private LocationGetter mGetter; 

    onCreate() { 
     mGetter = new LocationGetter(this); 
     locmanag.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mGetter); 
    } 

    public void somethingHappend() { 
     mGetter.getLat(); 
    } 
} 

public class LocationGetter implements LocationListener { 

    private final MyActivity mActivity; 
    public LocationGetter(MyActivity activity) { 
     mActivity= activity; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
    if(location != null){ 
     mLat = location.getLatitude(); 
     mLong = location.getLongitude(); 
     if (mActivity!= null) { 
      mActivity.somethingHappend(); 
     } 
    } 

    public long getLat() { 
     return mLat; 
    } 
} 

如果你想使用不同的活動你的LocationListener的,你應該實現一個共同的接口(如LocationListener的是)

class MyActivity extends Activity implements LocationGetter.MyCallback { 
    private LocationGetter mGetter; 

    onCreate() { 
     mGetter = new LocationGetter(this); 
     locmanag.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mGetter); 
    } 

    public void somethingHappend() { 
     mGetter.getLat(); 
    } 
} 

public class LocationGetter implements LocationListener { 
    public Interface MyCallback { 
     // you could pass lat + long here 
     public void somethingHappend(); 
    } 

    private final MyCallback mCallback; 
    public LocationGetter(MyCallback callback) { 
     mCallback = callback; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
    if(location != null){ 
     mLat = location.getLatitude(); 
     mLong = location.getLongitude(); 
     if (mCallback != null) { 
      mCallback.somethingHappend(); 
     } 
    } 

    public long getLat() { 
     return mLat; 
    } 
} 

除了創建新的回調接口,您還可以使用所有活動中的LocationListener。同樣適用,但不會告訴你這些回調是如何工作的:)

另一種可能性是使用例如處理程序(特別是如果其他東西是在不同的線程執行)

class MyActivity extends Activity implements Handler.Callback { 
    private Handler mHandler; 

    onCreate() { 
     mHandler = new Handler(this); 
     mGetter = new LocationGetter(mHandler); 
     locmanag.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mGetter); 
    } 

    public boolean handleMessage(Message msg) { 
     // msg.what would be 0, that's what we send 
     mGetter.getLat(); 
     // we are in UI thread here. safe to modify. 
     return true; 
    } 
} 

public class LocationGetter implements LocationListener { 

    private final Handler mHandler; 
    public LocationGetter(Handler handler) { 
     mHandler = handler; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
    if(location != null){ 
     mLat = location.getLatitude(); 
     mLong = location.getLongitude(); 
     if (mHandler != null) { 
      // you can send a lot more here 
      mHandler.sendEmptyMessage(0); 
     } 
    } 

    public long getLat() { 
     return mLat; 
    } 
} 

除了等待LocationListener的甚至是不可能的,如果你試圖從你的活動UI線程,因爲你更新的UI線程是被你的等待阻止。所以你永遠不會得到更新。

相關問題