2012-01-17 11 views
6

voglio sapere la forza della mia attuale torre di cella su Android segnale. Dopo alcune ricerche ho scoperto che dovrei usare un PhoneStateListener che ascolta un aggiornamento per ottenere il valore (modo strano per farlo IMHO).arresto Listener dopo il primo aggiornamento

quindi voglio ottenere il segnale non appena ho capito e fermare l'ascoltatore dopo. Questo è il codice che uso:

// 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()); 
      } 
    } 
} 

Vorrei fermare l'ascoltatore non appena mando un setStrength(value)

Qualche idea?

Grazie

risposta

10

Dal docs:

per annullare la registrazione di un ascoltatore, passare l'oggetto ascoltatore e impostare gli eventi argomento LISTEN_NONE (0).

Quindi, aggiungere questo al vostro ascoltatore:

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

Funziona bene, grazie –

Problemi correlati