65.9K
CodeProject is changing. Read more.
Home

Reject and Accept an Incoming Call

starIconstarIconstarIconstarIconstarIcon

5.00/5 (5 votes)

Apr 16, 2013

CPOL
viewsIcon

63053

This article helps you to reject and accept an incoming call for your Android device with functions.

Introduction

This article helps you to reject and accept an incoming call for your Android device with functions.

Using the code

You can use the rejectCall and acceptCall functions to automate your response for specific numbers or groups. The number coming from an incoming call can be caught with a phonestate listener, then the code can reject or you can accept incoming calls depend on this phone number.

For example, you can reject an incoming call with an unknown number or you can accept an incoming call coming from your favourite contacts automatically.

The code snippets show how to reject and accept an incoming call.

Reject an incoming call:

private void rejectCall(Context context) 
{

Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON); buttonDown.putExtra(Intent.EXTRA_KEY_EVENT,
new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
context.sendOrderedBroadcast(buttonDown, "android.permission.CALL_PRIVILEGED");}

Accept an incoming call:

public static void acceptCall(Context context) 
{
    Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
    buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, 
      new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
    context.sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
}