Click here to Skip to main content
Full site     10M members (33.9K online)    

Reject and Accept an Incoming Call

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");
 
    // froyo and beyond trigger on buttonUp instead of buttonDown
    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");
}

Accept an incoming call:

public static void acceptCall(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");
 
    // froyo and beyond trigger on buttonUp instead of buttonDown
    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");
}
 
You must Sign In to use this message board.
Search 
Per page   
-- There are no messages in this forum --

Last Updated 16 Apr 2013 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2013