Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you make the PhoneStateListener part of a service then you have a component of you app that always knows the current signal strength. The UI portion of the app can then query the service for the state on demand or have an update posted when the strength changes beyond some limit. For instance, signal strength changes rapidly - so only post an update if it changes by more than 10% or limit the updates by time.</p> <p>For instance (here update only gets sent of the objects of interest are changed (mSms or mMins) and only every 5th signal strength change. The application's Activity will sink this Intent. This is called by the service's PhoneStateListener overridden methods.</p> <pre><code>/* called from the phone state listener to check for any changes in the number of sms/calls since last time * and to raise a suitable intent if required. Check the time to avoid calling too fast. */ private void update() { final String key = getString(R.string.pref_periodstart_key); final String defVal = getString(R.string.periodstart_defaultValue); Thread thread = new Thread(null, new Runnable() { public void run() { Context context = getApplicationContext(); int start = Integer.valueOf(mPrefs.getString(key, defVal)); Date date = PhoneUsageInfo.getLastContractDate(start); int mins = PhoneUsageInfo.getMinutesUsedSince(context, date); int sms = PhoneUsageInfo.getSMSCountSince(context, date); boolean changed = (mins != mMins || sms != mSms); if (changed || mCount % 5 == 0) { mMins = mins; mSms = sms; Intent intent = new Intent(Constants.INTENT_USAGE_UPDATE); intent.putExtra(Constants.EXTRA_SMS_COUNT, sms); intent.putExtra(Constants.EXTRA_MIN_COUNT, mins); intent.putExtra(Constants.EXTRA_START_DATE, date); Log.d(TAG, String.format("service sending broadcast intent sms %d, mins %d (%s)", sms, mins, changed?"changed":"unchanged")); sendBroadcast(intent); } mCount += 1; } }, "PhoneUsageServiceUpdate"); thread.start(); } </code></pre>
 

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