2010-10-08 5 views
1

Je dois trouver un moyen de mesurer la puissance actuelle du signal du téléphone Android, sans avoir besoin d'enregistrer un PhoneStateListener que Dieu sait quand il renvoie l'asu réelle.Force du signal instantané

quelque chose comme:

int signal = getPhoneSignal(); 

toute plz aide?

merci!

+0

pouvez-vous essayer ma solution et voir si cela fonctionne? –

Répondre

1

Je ne pense pas qu'il existe un moyen de le faire directement. Mais vous pouvez enregistrer PhoneStateListener et enregistrer la dernière valeur mise à jour dans une variable et retourner/appeler ceci.

+0

j'ai essayé de le faire: L'activité est appelée à partir de thread principal PhoneStateListener est mis à jour de thread principal je ne peux pas obtenir l'activité d'attendre PhoneStateListener pour enregistrer sa première valeur de signal ... im ayant une impasse: S – Shatazone

2

Si vous avez de plus près sur les sources Android, vous verrez que, après l'enregistrement PhoneStateListener vous aurez une notification instantanée:

public void listen(PhoneStateListener listener, int events) { 
     String pkgForDebug = mContext != null ? mContext.getPackageName() : "<unknown>"; 
     try { 
      Boolean notifyNow = (getITelephony() != null); 
      mRegistry.listen(pkgForDebug, listener.callback, events, notifyNow); 
     } catch (RemoteException ex) { 
      // system process dead 
     } 
    } 

Vous pouvez créer votre propre minuterie et mise à jour de la minuterie enregistrer le nouvel auditeur et après Si vous recevez une mise à jour instantanée, supprimez-la en transmettant le même objet écouteur et définissez l'argument events sur LISTEN_NONE.

Bien sûr, je ne peux pas appeler cela la meilleure pratique, mais la seule alternative que je peux voir est de calculer la force du signal par vous-même en fonction des intensités du signal de getNeighboringCellInfo().

p.s. Not only God knows quand PhoneStateListener sera déclenchée;)

0
class Signal { 

    static volatile CountDownLatch latch; 
    static int asu; 
    private final static String TAG = Signal.class.getName(); 

    int getSignalStrength(Context ctx) throws InterruptedException { 
     Intent i = new Intent(TAG + ".SIGNAL_ACTION", Uri.EMPTY, ctx, 
       SignalListenerService.class); 
     latch = new CountDownLatch(1); 
     asu = -1; 
     ctx.startService(i); 
     Log.w(TAG, "I wait"); 
     latch.await(); 
     ctx.stopService(i); 
     return asu; 
    } 
} 

où:

public class SignalListenerService extends Service { 

    private TelephonyManager Tel; 
    private SignalListener listener; 
    private final static String TAG = SignalListenerService.class.getName(); 

    private static class SignalListener extends PhoneStateListener { 

     private volatile CountDownLatch latch; 

     private SignalListener(CountDownLatch la) { 
      Log.w(this.getClass().getCanonicalName(), "CSTOR"); 
      this.latch = la; 
     } 

     @Override 
     public void onSignalStrengthChanged(int asu) { 
      Signal.asu = asu; 
      latch.countDown(); 
     } 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Log.w(TAG, "Received : " + intent.getAction()); 
     Tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
     listener = new SignalListener(Signal.latch); 
     @SuppressWarnings("deprecation") 
     final int listenSs = PhoneStateListener.LISTEN_SIGNAL_STRENGTH; 
     Tel.listen(listener, listenSs); 
     return START_STICKY; 
    } 

    @Override 
    public void onDestroy() { 
     Log.w(TAG, "onDestroy"); 
     Tel.listen(listener, PhoneStateListener.LISTEN_NONE); 
     super.onDestroy(); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 
} 

Ceci est le code de travail. N'oubliez pas d'enregistrer vos services dans le manifeste et d'acquérir des autorisations. Il peut y avoir de meilleures/plus élégantes façons de faire ceci afin que les commentaires/corrections soient les bienvenus.

Questions connexes