
Log.e(LOG_TAG, "Unable to use the Telephony Manager directly.", e) Īctually, there is one slight problem. you probably want to set some failure state/go to fallback something more crazy, if anything else breaks ExceptionInInitializerError, if initialization failed NullPointerException, if something was a null value along the way InvocationTargetException, if the method threw itself

IllegalArgumentException, if the method expected other arguments IllegalAccessException, if the method is not accessible SecurityException, if the security manager is not happy NoSuchMethodException, if the answerRingingCall() is missing we catch it all as the following things could happen:

Tm.getClass().getMethod("answerRingingCall").invoke(tm) this will be easier for debugging later on

getSystemService(Context.TELEPHONY_SERVICE) TelephonyManager tm = (TelephonyManager) mContext And how does that look in code? // set the logging tag constant you probably want to change thisįinal String LOG_TAG = "TelephonyAnswer" You can also dig deeper into the specifics at Trail: The Reflection API if you are interested in doing so. If you are not familiar with reflection, you can quickly read What is reflection, and why is it useful?. How would we use this?Īs the method in question is hidden from the SDK applications use, you need to use reflection to dynamically examine and use the method during runtime. This means that unless you only support devices with Lollipop, which is probably a bad decision based on the tiny market share of it as of right now, you still need to provide fallback methods if going down this route.
#Nexus 5 incoming call screen for android
It is not available on 4.4.2_r1 as it was introduced only in commit 83da75d for Android 4.4 KitKat ( line 1537 on 4.4.3_r1) and later "reintroduced" in commit f1e1e77 for Lollipop ( line 3138 on 5.0.0_r1) due to how the Git tree was structured. It works as a bridge for ITelephony.answerRingingCall() which has been discussed on the interwebs and seems promising at the start. There is TelephonyManager.answerRingingCall() which is a hidden, internal method. Method 1: TelephonyManager.answerRingingCall()įor when you have unlimited control over the device. Throw new NullPointerException("tm = null") whether you want to handle this is up to you really getSystemService(Context.TELECOM_SERVICE) A basic invocation looks as follows then: TelecomManager tm = (TelecomManager) mContext You should follow requesting permissions at run time to obtain that new permission during run-time, as is standard on the newer Android versions.Īfter having obtained the permission, your app just has to simply call the TelecomManager's acceptRingingCall method. You should check system version at runtime if you are supporting older Android versions so that you can encapsulate this new API call while maintaining support for those older Android versions. As the name of permission suggests, holding it allows your app to programmatically accept incoming calls through a proper API call without any hacking around the system using reflection or simulating the user. Starting with Android 8.0 Oreo, the PHONE permission group also contains the ANSWER_PHONE_CALLS permission. The backward compatible methods are still described below.

Even though the question was originally asked for Android L support, people still seem to be hitting this question and answer, so it is worth describing the improvements introduced in Android 8.0 Oreo.
