Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>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; } } </code></pre> <p>where :</p> <pre><code>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; } } </code></pre> <p>This is working code. Do not forget to register your services in the manifest and acquire permissions. There may be better/ more elegant ways of doing this so comments/corrections welcome. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload