user6829088
user6829088

Reputation:

addAction to IntentFilter to handle Phone State Change

I want to listen to an incoming call.

I know that you have to give permissions inside AndroidManifest and set the receiver with

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

and

<receiver ... 
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />...


But, in my project, I have the receiver set by code, with

IntentFilter filter = new IntentFilter();
filter.addAction( ... );
registerReceiver(broadcastReceiver, filter);

Where inside addAction I put TelephonyManager.ACTION_PHONE_STATE_CHANGED
and I create the BroadcastReceiver with

private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) { ...

Then, inside it, I take the action from the intent with String action = intent.getAction(); and I check which action is it.

It works great for Bluetooth

filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);

And

switch (action) {
    case BluetoothAdapter.ACTION_DISCOVERY_STARTED: ...

But it does not checks TelephonyManager.ACTION_PHONE_STATE_CHANGED.

So, my questions are:
How do you set the action (addAction()) to listen to state changes?
Is TelephonyManager.ACTION_PHONE_STATE_CHANGED not right?
If I would give up and set the receiver inside AndroiManifest, how would I set the filters for Bluetooth? I'm using

BluetoothDevice.ACTION_FOUND
BluetoothAdapter.ACTION_DISCOVERY_STARTED
BluetoothAdapter.ACTION_DISCOVERY_FINISHED

Upvotes: 1

Views: 4372

Answers (2)

Amani
Amani

Reputation: 3977

Create this object in onCreate() method:

tMg =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

listener = new PhoneStateListener{

    onCallStateChanged(int state, String incomingNumber){
         switch(state){
               case EXTRA_STATE_RINGING:
                   // phone is ringing
               break;
               case EXTRA_STATE_OFFHOOK:
                  // phone is offhook
               break;
               case EXTRA_STATE_IDLE:
                 // phone is idle
               break;
         }
    }

}

in onStart() register your listener:

tMg.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

in onStop() unregister your listener:

tMg.listen(listener,  PhoneStateListener.LISTEN_NONE);

Upvotes: 1

tompadre
tompadre

Reputation: 807

First register reciever in your activity

this.registerReceiver(this.broadcastReceiver, new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED));

and this is the broadcastReceiver:

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        if (state == null) {

            //Outgoing call
            String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            Log.e("tag", "Outgoing number : " + number);

        } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {

            Log.e("tag", "EXTRA_STATE_OFFHOOK");

        } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {

            Log.e("tag", "EXTRA_STATE_IDLE");

        } else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {

            //Incoming call
            String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Log.e("tag", "Incoming number : " + number);

        } else
            Log.e("tag", "none");
    }
};

Upvotes: 2

Related Questions