Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Android

Reject and Accept an Incoming Call

5.00/5 (5 votes)
15 Feb 2014CPOL 63.1K  
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:

Java
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:

Java
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");
} 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)