6

In my application, it would be nice to intercept when the user rejects a call, i.e. choose to send busy tone. Is there any way, besides being notified when there is an incoming call, to do this? I have not found anything in the docs regarding this. Is this information stored in the call log, i.e. can I poll the call log to see if a call was rejected or answered?

Cœur
  • 37,241
  • 25
  • 195
  • 267
AndersG
  • 365
  • 4
  • 12

3 Answers3

5

Hey. You could use the PhoneStateListener and CallLog classes to query the recent calls list to find rejected calls.

http://developer.android.com/reference/android/provider/CallLog.html http://developer.android.com/reference/android/telephony/PhoneStateListener.html

Alternatively you can monitor broadcasts on android.intent.action.NEW_OUTGOING_CALL and android.intent.action.NEW_INCOMING_CALL just put android.permission.PROCESS_INCOMING_CALLS and android.permission.PROCESS_OUTGOING_CALLS in your manifest.

Rob
  • 7,039
  • 4
  • 44
  • 75
  • There are more types outside of the standard Incoming(1), Outgoing(2) and Missed(3). So far I have encountered 4 (VoiceMail), 5 (Rejected) and 6 (Blocked List). The duration will be zero for 3, 5 and 6, have not tried the Voicemail type in code yet. – a4p-jms Jun 29 '12 at 15:42
  • @Rob, Hear I implemented your idea to get status of outgoing call,[Hear is my simple class](https://gist.github.com/2decd4e406746b93d187), I am also give android.permission.PROCESS_OUTGOING_CALLS" android:name="android.permission.READ_PHONE_STATE" permissions. and in manifast file, I register the receiver with android.intent.action.NEW_OUTGOING_CALL action My Need is when I call a number, If i got no answer or reject my call than I have to show a Toast, Please give me some idea, – Ashish Dwivedi Dec 24 '12 at 10:24
1

After a long time, Search and logic to solve this problem, I get the solution:

Problem: If i call a person, if my call is not accepted by the person,in my side i have to show a Toast is my call is rejected

Solution : Create a receiver which is register by two action

android.intent.action.NEW_OUTGOING_CALL android.intent.action.PHONE_STATE

Both action are necessary.

For this you have to take given permission

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

Manifast.xml

<receiver android:name="Call_Receiver">
            <intent-filter>

                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
                <action android:name="android.intent.action.PHONE_STATE"/>

            </intent-filter>
        </receiver>

And your BroadcastReceiver class

*Call_Receiver.java*

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

/** 
 * 
 */
public class Call_Receiver extends BroadcastReceiver {

    private Context ctx;


    @Override
    public void onReceive(Context context, Intent intent) {
        ctx = context;
        OutgoingIncomingCallListener phoneListener = new OutgoingIncomingCallListener();
        TelephonyManager telephony = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);

        if (intent.getAction()
                .equals("android.intent.action.NEW_OUTGOING_CALL")) {

            Log.i("", " :::::::::::: inside onReceive if &&&&&&  :::::::::::::");

            telephony.listen(phoneListener,
                    PhoneStateListener.LISTEN_CALL_STATE);

        }
    }

    class OutgoingIncomingCallListener extends PhoneStateListener {
        public boolean wasRinging;

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                Log.i("", "  ************   RINGING  ********");
                wasRinging = true;
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.i("", "  ***********  OFFHOOK  ********");

                if (!wasRinging) {
                    // Start your new activity
                    Log.i("", "  ***********  if  ********");
                } else {
                    // Cancel your old activity
                    Log.i("", "  ***********  else  ********");
                }

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

                if (wasRinging) {
                    Toast.makeText(ctx,
                            "Your call is disconnected by receiver", 10000)
                            .show();
                }
                wasRinging = true;
                break;
            }
        }
    }
}

This will show you a Toast when you call is not accepting.

Ashish Dwivedi
  • 8,048
  • 5
  • 58
  • 78
0

what if use android.intent.action.NEW_INCOMING_CALL?

When you reject incoming call, callstate changes from ringing to idle. This means you rejected.

If call state changes from OFFHOOK to idle it means you answered a call.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Alexe
  • 1