Click here to Skip to main content
Click here to Skip to main content

Reject and Accept an Incoming Call

By , 16 Apr 2013
 

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

License

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

About the Author

Yildirim Kocdag
Software Developer (Senior)
Turkey Turkey
Member
Dr. Yildirim Kocdag is a Computer Engineer from Istanbul. Currently using Android, Objective-c, c#, vb.net, asp.net, javascript, SQL and Oracle. His favourite areas in Computer Science are Compilers, Expert Systems, Digital Image Processing, AI and Extreme Programming.
ykocdag@yahoo.com

Comments and Discussions

Comment 0 messages have been posted for this article Visit http://www.codeproject.com/Tips/578817/Reject-and-Accept-an-Incoming-Call to post and view comments on this article, or click here to get a print view with messages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130513.1 | Last Updated 16 Apr 2013
Article Copyright 2013 by Yildirim Kocdag
Everything else Copyright © CodeProject, 1999-2013
Terms of Use