FileDocCategorySizeDatePackage
PhoneStateIntentReceiver.javaAPI DocAndroid 1.5 API6964Wed May 06 22:42:00 BST 2009com.android.internal.telephony

PhoneStateIntentReceiver

public final class PhoneStateIntentReceiver extends android.content.BroadcastReceiver
DO NOT USE THIS CLASS: Use android.telephony.TelephonyManager and PhoneStateListener instead.

Fields Summary
private static final String
LOG_TAG
private static final boolean
DBG
public static final String
INTENT_KEY_ASU
private static final int
NOTIF_PHONE
private static final int
NOTIF_SERVICE
private static final int
NOTIF_SIGNAL
private static final int
NOTIF_MAX
Phone.State
mPhoneState
android.telephony.ServiceState
mServiceState
int
mAsu
private android.content.Context
mContext
private android.os.Handler
mTarget
private android.content.IntentFilter
mFilter
private int
mWants
private int
mPhoneStateEventWhat
private int
mServiceStateEventWhat
private int
mLocationEventWhat
private int
mAsuEventWhat
Constructors Summary
public PhoneStateIntentReceiver()


      
        super();
        mFilter = new IntentFilter();
    
public PhoneStateIntentReceiver(android.content.Context context, android.os.Handler target)

        this();
        setContext(context);
        setTarget(target);
    
Methods Summary
public booleangetNotifyPhoneCallState()

        return ((mWants & NOTIF_PHONE) != 0);
    
public booleangetNotifyServiceState()

        return ((mWants & NOTIF_SERVICE) != 0);
    
public booleangetNotifySignalStrength()

        return ((mWants & NOTIF_SIGNAL) != 0);
    
public Phone.StategetPhoneState()

        if ((mWants & NOTIF_PHONE) == 0) {
            throw new RuntimeException
                ("client must call notifyPhoneCallState(int)");
        }
        return mPhoneState;
    
public android.telephony.ServiceStategetServiceState()

        if ((mWants & NOTIF_SERVICE) == 0) {
            throw new RuntimeException
                ("client must call notifyServiceState(int)");
        }
        return mServiceState;
    
public intgetSignalStrength()
Returns current signal strength in "asu", ranging from 0-31 or -1 if unknown For GSM, dBm = -113 + 2*asu 0 means "-113 dBm or less" 31 means "-51 dBm or greater"

return
signal strength in asu, -1 if not yet updated Throws RuntimeException if client has not called notifySignalStrength()

        if ((mWants & NOTIF_SIGNAL) == 0) {
            throw new RuntimeException
                ("client must call notifySignalStrength(int)");
        }

        return mAsu;
    
public intgetSignalStrengthDbm()
Return current signal strength in "dBm", ranging from -113 - -51dBm or -1 if unknown

return
signal strength in dBm, -1 if not yet updated Throws RuntimeException if client has not called notifySignalStrength()

        if ((mWants & NOTIF_SIGNAL) == 0) {
            throw new RuntimeException
                ("client must call notifySignalStrength(int)");
        }

        int dBm = -1;

        if (mAsu != -1) {
            dBm = -113 + 2*mAsu;
        }

        return dBm;
    
public voidnotifyPhoneCallState(int eventWhat)

        mWants |= NOTIF_PHONE;
        mPhoneStateEventWhat = eventWhat;
        mFilter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
    
public voidnotifyServiceState(int eventWhat)

        mWants |= NOTIF_SERVICE;
        mServiceStateEventWhat = eventWhat;
        mFilter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
    
public voidnotifySignalStrength(int eventWhat)

        mWants |= NOTIF_SIGNAL;
        mAsuEventWhat = eventWhat;
        mFilter.addAction(TelephonyIntents.ACTION_SIGNAL_STRENGTH_CHANGED);
    
public voidonReceive(android.content.Context context, android.content.Intent intent)

        String action = intent.getAction();

        try {
            if (TelephonyIntents.ACTION_SIGNAL_STRENGTH_CHANGED.equals(action)) {
                mAsu = intent.getIntExtra(INTENT_KEY_ASU, mAsu);
                if (DBG) Log.d(LOG_TAG, "onReceiveIntent: set asu=" + mAsu);
                
                if (mTarget != null && getNotifySignalStrength()) {
                    Message message = Message.obtain(mTarget, mAsuEventWhat);
                    mTarget.sendMessage(message);
                }
            } else if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) {
                if (DBG) Log.d(LOG_TAG, "onReceiveIntent: ACTION_PHONE_STATE_CHANGED, state="
                               + intent.getStringExtra(Phone.STATE_KEY));
                String phoneState = intent.getStringExtra(Phone.STATE_KEY);
                mPhoneState = (Phone.State) Enum.valueOf(
                        Phone.State.class, phoneState);

                if (mTarget != null && getNotifyPhoneCallState()) {
                    Message message = Message.obtain(mTarget,
                            mPhoneStateEventWhat);
                    mTarget.sendMessage(message);
                }
            } else if (TelephonyIntents.ACTION_SERVICE_STATE_CHANGED.equals(action)) {
                mServiceState = ServiceState.newFromBundle(intent.getExtras());

                if (mTarget != null && getNotifyServiceState()) {
                    Message message = Message.obtain(mTarget,
                            mServiceStateEventWhat);
                    mTarget.sendMessage(message);
                }
            }
        } catch (Exception ex) {
            Log.e(LOG_TAG, "[PhoneStateIntentRecv] caught " + ex);
            ex.printStackTrace();
        }
    
public voidregisterIntent()

        mContext.registerReceiver(this, mFilter);
    
public voidsetContext(android.content.Context c)

        mContext = c;
    
public voidsetTarget(android.os.Handler h)

        mTarget = h;
    
public voidunregisterIntent()

        mContext.unregisterReceiver(this);