Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically redirect incoming calls to voice mail when my Calendar has a busy event

Being new to Android development I have been researching StackOverflow for an answer to this question: How to mute the phone ringer for all incoming messages and calls when I am supposed to be in a Meeting as indicated by my Calendar. I am able to access the calendar and able to check if present time is within the times set for an event. But how do I send any incoming phone calls to voice mail (and send a text message to caller). I have gone through lots of questions here:

"How to detect incoming calls, in an Android device?"

"Incoming call broadcast receiver not working (Android 4.1)"

"How to detect incoming call with the help of Broadcast Receiver?"

"Custom incoming/outgoing call screen in Android"

"how to make an incoming call from our application"

"Android: Taking complete control of phone(kiosk mode), is it possible? How?"

"How to disconnect incoming call in android by programatically"

The ITelephony using AIDL looked promising untill I found out that it no longer works under Android 4.2 etc due to security issues.

At the moment the answer: "How to detect incoming calls, in an Android device?" seems to be better simply because it seems to handle incoming calls in an elegant way - but the answer does not talk about how to (a) Silence the Ringer, (b) End the Call/Divert it etc - the example explains how to determine what state the phone is in at any given time. The other drawback seems to be I am struggling to unregister the listener function which means even after the example app has been terminated, the listener still seems to remain active and jumps into action whenever there is a call. Only way to stop that seems to be to restart the phone - which is very annoying.

The other aspect is that most of these questions are a year or more old and some of the old answers are no longer valid - as I mentioned earlier - the ITelephony looked promising but no longer works with new phones.

The Google documentation seems to suggest that a program can programmatically wrap up an answer to an incoming call in a SMS Text message by sending an Intent etc - but no examples anywhere and looks like the Google's documentation may be out of date (or I am reading an old version of it on the internet - though don't know how it is possible since I am using Google Search to find an answer.)

like image 320
VLBaindoor Avatar asked Apr 02 '14 00:04

VLBaindoor


1 Answers

Silencing the ringer and stopping the incoming / outgoing call is still possible.

First, the example from "How to detect incoming calls, in an Android device?" is the correct way to detect the incoming call.

Now, for silencing the ringer and stopping the call:

First, create an interface named "ITelephony" under the "com.android.internal.telephony" package:

http://i.stack.imgur.com/UQoVc.png

And add this code to the interface:

public interface ITelephony
{
    boolean endCall();
    void answerRingingCall();
    void silenceRinger();
}

Then, on the method where you want to silence the ringer and stop the call, add this code:

//TelephonyManager
TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

// Get the getITelephony() method
Class c = Class.forName(telephonyManager.getClass().getName());
                        Method m = c.getDeclaredMethod("getITelephony");

// Disable access check
m.setAccessible(true);

// Invoke getITelephony() to get the ITelephony interface
ITelephony telephonyInterface = (ITelephony) m.invoke(telephonyManager);

//Invoke silenceRinger
telephonyInterface.silenceRinger();

// Invoke endCall()
telephonyInterface.endCall();

Finally, in your manifest, you need to add these permissions:

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" tools:ignore="ProtectedPermissions" />

I have tested this code on many devices and it works, if you have any problem, feel free to ask me :)

like image 84
Jorge Corea Avatar answered Oct 13 '22 08:10

Jorge Corea