Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you receive outgoing call in broadcastreceiver

I am trying to identify and transfer to an activity after an outgoing call is initiated. I used ACTION_NEW_OUTGOING_CALL in the Intent filter. However how csn I identify that the call is outgoing. I did this for an incoming call (as seen below) but what can I use instead of EXTRA_STATE_RINGING.

public class OutgoingBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
        {
            Intent i = new Intent(context, OutgoingCallScreenDisplay.class);
            i.putExtras(intent);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }
}

enter image description here

like image 206
user1163234 Avatar asked Mar 05 '12 15:03

user1163234


People also ask

How do I find incoming and outgoing calls on Android?

You need a BroadcastReceiver for ACTION_PHONE_STATE_CHANGED This will call your received whenever the phone-state changes from idle,ringing,offhook so from the previous value and the new value you can detect if this is an incoming / outgoing call.

How does broadcast receive work?

Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.


1 Answers

public class OutgoingBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
           // If it is to call (outgoing)
           Intent i = new Intent(context, OutgoingCallScreenDisplay.class);
           i.putExtras(intent);
           i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           context.startActivity(i);
        }
    }
}

ACTION_NEW_OUTGOING_CALL is a constant declare in the Intent class, not in TelephonyManager. When an outgoing call appears, then the system broadcasts an intent with this constant. If you really want to catch an outgoing call by using TelephonyManager then do this:

TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);   
tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
PhoneStateListener listener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        // TODO Auto-generated method stub

        super.onCallStateChanged(state, incomingNumber);
        switch(state) {
            case TelephonyManager.CALL_STATE_IDLE:
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                if(incomingNumber==null) {
                    //outgoing call
                } else {
                    //incoming call
                }
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                if(incomingNumber==null) {
                    //outgoing call
                } else {
                    //incoming call
                }
                break;
        }
    }
};
like image 172
ρяσѕρєя K Avatar answered Sep 20 '22 07:09

ρяσѕρєя K