2012-01-17 58 views
6

我想知道當前Android手機信號塔的信號強度。經過一些研究後,我發現,我應該使用一個PhoneStateListener來偵聽更新以獲取值(奇怪的方式來做那個恕我直言)。第一次更新後停止監聽器

因此,我想一聽到信號就立即得到信號,然後停止收聽。這是我的代碼使用方法:

// object containing the information about the cell 
MyGSMCell myGSMCell = new MyGSMCell(cid,lac); 
// object listening for the signal strength 
new GetGsmSignalStrength(myGSMCell, context); 

... 

public class GetGsmSignalStrength { 

    private MyGSMCell callback; 
    private TelephonyManager telMan; 
    private MyPhoneStateListener myListener; 

    public GetGsmSignalStrength(MyGSMCell callback, Context context) { 
      this.callback = callback; 
      this.myListener = new MyPhoneStateListener(); 
      this.telMan = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
      this.telMan.listen(this.myListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS); 

    } 

    private class MyPhoneStateListener extends PhoneStateListener 
    { 
      public void onSignalStrengthsChanged(SignalStrength signalStrength) 
      { 
        // get the strength 
        super.onSignalStrengthsChanged(signalStrength); 
        // return it to the MyGSMCell object to set the value 
        callback.setStrength(signalStrength.getGsmSignalStrength()); 
      } 
    } 
} 

我想盡快停止監聽,因爲我送setStrength(value)

任何想法?

謝謝

回答

10

docs

要取消註冊監聽器,通過監聽對象和 參數設置事件LISTEN_NONE(0)。

因此,添加到您的聽衆:

telMan.listen(myListener, PhoneStateListener.LISTEN_NONE); 
+0

效果很好,謝謝 – 2012-01-17 23:32:54