How to reject a call programmatically in android

In my application, I will save the contact list.

Any calls from contacts in the list will be deleted. They will be displayed on missed calls, but the phone will not ring.

+5
android telephony
Sep 08 '11 at 12:17
source share
2 answers

First create this interface:

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

Then create this class that extends BroadcastReceiver

 public class IncomingCallReceiver extends BroadcastReceiver { private ITelephony telephonyService; private String blacklistednumber = "+458664455"; @Override public void onReceive(Context context, Intent intent) { TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); try { Class c = Class.forName(tm.getClass().getName()); Method m = c.getDeclaredMethod("getITelephony"); m.setAccessible(true); ITelephony telephonyService = (ITelephony) m.invoke(tm); Bundle bundle = intent.getExtras(); String phoneNumber = bundle.getString("incoming_number"); Log.e("INCOMING", phoneNumber); if ((phoneNumber != null) && phoneNumber.equals(blacklistednumber)) { telephonyService.silenceRinger(); telephonyService.endCall(); Log.e("HANG UP", phoneNumber); } } catch (Exception e) { e.printStackTrace(); } } 

This will block only one number, but you will get a point.

In the manifest add this:

 <receiver android:name=".IncomingCallReceiver"> <intent-filter android:priority="999"> <action android:name="android.intent.action.PHONE_STATE" /> </intent-filter> </receiver> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS" /> 
+15
Sep 08 2018-11-12T00:
source share

Download the ITelephony class from here .

Then put it in the package (create a new package) com.android.internal.telephony. Then import the package into the appropriate class and use the endCall() method to reject the call

+2
Oct 11 '14 at 6:41
source share



All Articles