End call in android programmatically

I see many questions that cannot be completed programmatically in Android. At the same time, I see a lot of applications for dialing on the googleplay market, where you can activate the call and drop it. How do they work?

Change I read somewhere that my application should be a system application. Then, how to make it a system, and what is the difference between system and user applications?

+30
android phone-call
Aug 05 '13 at 18:28
source share
8 answers

You do not need to be a system application. First, create the com.android.internal.telephony package in your project and put it in a file called " ITelephony.aidl ":

 package com.android.internal.telephony; interface ITelephony { boolean endCall(); void answerRingingCall(); void silenceRinger(); } 

Once you have this, you can use this code to end the call:

 TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); Class clazz = Class.forName(telephonyManager.getClass().getName()); Method method = clazz.getDeclaredMethod("getITelephony"); method.setAccessible(true); ITelephony telephonyService = (ITelephony) method.invoke(telephonyManager); telephonyService.endCall(); 

You can use this inside PhoneStateListener, for example. For this to work, you need permissions in the manifest:

 <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 

Edit: sorry for the awful formatting, I still can’t figure out how to make code blocks correctly here: /

+44
Aug 05 '13 at 18:44
source share

For Android P (since beta 2) and later, finally, there is a formal API for endCall:

https://developer.android.com/reference/android/telecom/TelecomManager#endCall ()

Permission ANSWER_PHONE_CALLS is required in the manifest:

 <uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" /> 

By permission, for API level 28 or higher:

 TelecomManager tm = (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); if (tm != null) { boolean success = tm.endCall(); // success == true if call was terminated. } 

At the same time, the original endCall() in TelephonyManager now protected by the MODIFY_PHONE_STATE permission and can no longer be called by non-system applications by reflection without permission (otherwise, a security exception will be raised).

+9
Jun 07 '18 at 7:42
source share

Along with adding an android telephony interface and a broadcast receiver, you will also need to add an android manifest receiver entry with the android.intent.action.PHONE_STATE action for the receiver you want to handle the intent with.

You will get a compile time error if you add

 uses-permission android:name="android.permission.MODIFY_PHONE_STATE` 

in the manifest file. But even if we delete it, it will automatically reject incoming calls.

+1
02 Oct '14 at 14:23
source share

For information.

May be useful in some situations. There is a potential workaround using the InCallService class. Most of the necessary information is here. https://developer.android.com/reference/android/telecom/InCallService.html#onCallRemoved(android.telecom.Call)

This requires setting your application as the default phone application and providing the following.

 <uses-permission android:name="android.permission.CALL_PHONE" /> 

If you implement your own class that extends InCallService , when the call starts, it binds to your application and you get information about the call through onCallAdded() . Then you can just call.disconnect() and the call will end.

+1
Sep 21 '18 at 7:35
source share

SilenceRinger() does not work for versions of Android 2.3+. Just comment on this, other code will work fine. Hope this works for you!

0
May 2 '14 at 8:11
source share
 public static boolean isCallActive(Context context){ AudioManager manager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); if(manager.getMode()==AudioManager.MODE_IN_CALL || manager.getMode()==AudioManager.MODE_IN_COMMUNICATION){ return true; } return false; } 
0
Jan 23 '17 at 21:00
source share
 To end call in older version then 9.0 use this: TelephonyManager tm = (TelephonyManager)context.getSystemService(TELEPHONY_SERVICE); Method m1 = null; try { m1 = tm.getClass().getDeclaredMethod("getITelephony"); } catch (NoSuchMethodException e) { e.printStackTrace(); } m1.setAccessible(true); Object iTelephony = null; try { iTelephony = m1.invoke(tm); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } Method m3 = null; try { m3 = iTelephony.getClass().getDeclaredMethod("endCall"); } catch (NoSuchMethodException e) { e.printStackTrace(); } try { m3.invoke(iTelephony); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } & for pie TelecomManager telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE); if (telecomManager != null) { return telecomManager.endCall(); } Make sure your compile SDK version is 28 
0
Feb 01 '19 at 7:01
source share

Just to add @headuck to the answer. For API 28, you also need to add:

 <uses-permission android:name="android.permission.READ_CALL_LOG"/> 

then request permission in your activity. In total, I requested these permissions for it to work (READ_PHONE_STATE, CALL_PHONE, ANSWER_PHONE_CALLS, READ_CONTACTS, READ_CALL_LOG)

0
Jul 18 '19 at 11:34
source share



All Articles