arul
arul

Reputation: 300

track the incoming call action

I know this is a asked question. By using broadcast receiver and using android.intent.action.PHONE_STATE

in receiver tag, you can know the actions of a phone. But how can I identify whether it is an incoming call or a outgoing call?

here is my code

@Override
public void onReceive(Context context, Intent intent) 
{
    this.context = context ;
    System.out.println(":::called onReceiver:::");
    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    telephony.listen(phoneCallListener, PhoneStateListener.LISTEN_CALL_STATE);
}
private final PhoneStateListener phoneCallListener = new PhoneStateListener() 
{
        @Override
        public void onCallStateChanged(int state, String incomingNumber) 
        {
            switch(state)
            {
            case TelephonyManager.CALL_STATE_RINGING:
                 isRinging = true;
                 break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                 if (isRinging) 
                 {
                     hasAttended = true ;
                     isRinging = false;
                 } 
                 break;
            case TelephonyManager.CALL_STATE_IDLE:
                 if (hasAttended)
                 {
                     isCallEnded = true ;
                     hasAttended = false;
                 }
                 break;
            }
            if(isCallEnded)
            {
                isCallEnded=false;
                Intent callIntent = new Intent(context.getApplicationContext(),MyActivity.class);
                callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(callIntent);
            }
            super.onCallStateChanged(state, incomingNumber);
        }
    };

here each time a phoneCallListener object is created and increases the calling rate of onCallStateChanged by one each time..

Upvotes: 1

Views: 2973

Answers (1)

Hip Hip Array
Hip Hip Array

Reputation: 4753

This is a handy tutorial that uses both broadcast receiver and using android.intent.action.PHONE_STATE that creates different toasts to appear depending on the calls state. When you get this working you will be able to manipulate the code to do what you want when a call is either incoming or outgoing

First your class must extend BroadcastReceiver

Next you have to create a method that will listen to the phones state... PhoneStateListener which will listen to when the Phone state changes

Then do a simple switch case to catch the call depending on if it is an incoming call or an outgoing call

EDIT

All you need to do is write some code to check if the previous state was 'ringing'. If the current state is idle and the previous state was ringing, they cancelled/missed the call. If the current state is offhook and the previous state was ringing, they answered the call.

switch(state){
        case TelephonyManager.CALL_STATE_RINGING:
             Log.i(LOG_TAG, "RINGING");
             wasRinging = true;
             break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
             Log.i(LOG_TAG, "OFFHOOK");

             if (!wasRinging) {
                 // Start your new activity
             } else {
                 // Cancel your old activity
             }

             // this should be the last piece of code before the break
             wasRinging = true;
             break;
        case TelephonyManager.CALL_STATE_IDLE:
             Log.i(LOG_TAG, "IDLE");
             // this should be the last piece of code before the break
             wasRinging = true;
             break;
    }

Upvotes: 1

Related Questions