EvilGuy
EvilGuy

Reputation: 1

Android - How to detect if user rejected an incoming call?

I'm making an Android app with incoming calls. Is there any way in my app, to know if user rejected any incoming call before answering it?

Upvotes: 0

Views: 1376

Answers (2)

experiment.pl
experiment.pl

Reputation: 61

It's been a while since you asked, but I think it might be useful for future reference for others.

If you extract data from Android's calllog (e.g to XML or in your case to a variable in your app) you have two fields of interest:

1) numbertype e.g 1 for Incoming, 2 for Outgoing, 3 for missed

2) duration (in seconds)

Rejected number is (as you correctly mentioned) treated as numbertype=1. However if you combine numbertype=1 AND duration=0 (since any call answered will have duration>0) then this hopefully solves your problem.

I'm using Android 6, not sure if the types changed since then, but with me the above method works 99.9% of the time. Never managed to hang up the phone in less than a second :)

Upvotes: 0

Avijit Karmakar
Avijit Karmakar

Reputation: 9388

First, you have to add a permission in manifest file.

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

Then you have to register a broadcast receiver in the manifest file.

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

Then write in your broadcast receiver's onReceive method:

public void onReceive(Context context, Intent intent) {
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); 
    if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
        //If the phone is **Ringing**
    }else if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK){
        //If the call is **Received**
    }else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
        //If the call is **Dropped** or **Rejected**
    }
}

If the state changes from TelephonyManager.EXTRA_STATE_RINGING to TelephonyManager.EXTRA_STATE_IDLE, then it will be a missed call.

Like this you have to check the conditions.

Please put a log in those conditions. Then see the logs. I think you will get your answer.

Upvotes: 1

Related Questions