How to Reject a call programatically in android
Asked Answered
K

2

11

In my app I will maintain a list of contacts.

Any calls from contacts in the list will be dropped. They will show under missed calls but the phone will not ring.

Kalidasa answered 8/9, 2011 at 12:17 Comment(3)
ContentObserver is word where your can best practiceJabin
possible duplicate of How to block calls in androidAtheistic
MODIFY_PHONE_STATE permission is for system apps only (for android 2.3 and above). If you want to use app only for phone you have access to, you can make your app system appLumberjack
D
19

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 only block that single phonenumber, but you get the point.

In your 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" />
Darius answered 8/9, 2011 at 12:35 Comment(11)
hi i did that. But notihing happened. How to create that interface what i mean normal interface or aidl fileKalidasa
just create a normal intercafe. But you might need to place it in a package called: com.android.internal.telephony try that.Darius
even it is not working i think we have to generate aidl file. But do not know how to do that.Kalidasa
No you dont have to do that. This works just fine for me. Make sure the phonenumber you want to block is the same as the one that is incoming. You can check it in LogCat under.Darius
hi Larsen its working i checked now. Thanks for your quick response.Kalidasa
getting NoSuchMethodException at Method m = c.getDeclaredMethod("getITelephony");.. How to avoid it????Cilurzo
How would you add the "android.permission.MODIFY_PHONE_STATE" permission?Jobey
@Jobey please use this <uses-permission tools:ignore="ProtectedPermissions" android:name="android.permission.MODIFY_PHONE_STATE" />Holstein
Any idea how to achieve it on android oreo , I tested the above code that works before oreo but not on android oreo. Please share what is needed to change.ThanksHolstein
Permission MODIFY_PHONE_STATE is only granted to system appsPenchant
this code 6.0 work perfect but 7.0 or above not work so any other solution?Avalanche
E
2

Download the class of ITelephony from here.

Then put it in a package (make a new package) of com.android.internal.telephony. Then import the package to the appropriate class and for rejecting a call use the endCall() method

Empiricism answered 11/10, 2014 at 6:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.