TiGer
TiGer

Reputation: 5949

Service and a BroadCastReceiver

I have seen several examples of how to implement a BroadCastReceiver, but how should I implement a Service that has to react to some pending Intent (for example incoming phone call)... Actually I was wondering about the same "problem" but in an Activity.. You obviously have a class which extends a Service or an Activity) so it cannot also extend BroadCastReceiver... It looks like we cannot make "platform-aware" services and/or Activties?

Upvotes: 9

Views: 25185

Answers (3)

Witold Graca
Witold Graca

Reputation: 264

Actually you can react to incoming phone call just by adding listener to TelephonyManager

You define PhoneStateListener in your Service/Activity

private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);

        switch (state) {
        case TelephonyManager.CALL_STATE_OFFHOOK:
                     break;
        case TelephonyManager.CALL_STATE_RINGING:
                     break;
        case TelephonyManager.CALL_STATE_IDLE:
                     break;
        }
    }

};

Then in onCreate method:

mTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

Finaly you clear the listener in onDestroy:

mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);

So much simpler in this case.

Upvotes: 4

m6tt
m6tt

Reputation: 4223

To register an activity to receive a certain intent you need to:

// Flag if receiver is registered 
private boolean mReceiversRegistered = false;

// I think this is the broadcast you need for something like an incoming call
private String INCOMING_CALL_ACTION = "android.intent.action.PHONE_STATE";

// Define a handler and a broadcast receiver
private final Handler mHandler = new Handler();
private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    // Handle reciever
    String mAction = intent.getAction();

    if(mAction.equals(INCOMING_CALL_ACTION) {
      // Do your thing   
    }
}

@Override
protected void onResume() {
  super.onResume();

  // Register Sync Recievers
  IntentFilter intentToReceiveFilter = new IntentFilter();
  intentToReceiveFilter.addAction(INCOMING_CALL_ACTION);
  this.registerReceiver(mIntentReceiver, intentToReceiveFilter, null, mHandler);
  mReceiversRegistered = true;
}

@Override
public void onPause() {
  super.onPause();

  // Make sure you unregister your receivers when you pause your activity
  if(mReceiversRegistered) {
    unregisterReceiver(mIntentReceiver);
    mReceiversRegistered = false;
  }
}

Then you will also need to add an intent-filter to your manifest:

 <activity android:name=".MyActivity" android:label="@string/name" >
   <intent-filter> 
     <action android:name="android.intent.action.PHONE_STATE" /> 
   </intent-filter>
 </activity>

Upvotes: 12

reflog
reflog

Reputation: 7645

you can create an inner class

class A extends Activity {
 BroadcastReceiver r = new BroadcastReceiver(){
   // code to handle broadcase
 }
}

that class will receive events, which you can pass to main handler, or just call some outer methods

Upvotes: 4

Related Questions