Jenisha
Jenisha

Reputation: 51

Incoming Call Blocking in Android

I am able to block incoming calls in android but problems it rings for fraction of seconds before disconnection of time. How can i directly disconnect phone without single ringing?

I have added permission in Manifest file:

<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-feature android:name="android.hardware.telephony" />

Then Create IDL interface for getting core Telephony service.

 package com.android.internal.telephony;
  interface ITelephony {

  boolean endCall();

  void answerRingingCall();

  void silenceRinger();
}

Then made broadcast receiver for incoming call.

public class PhonecallReceiver extends BroadcastReceiver {

Context context = null;

@Override
public void onReceive(Context context, Intent intent) {

      Log.i(TAG, "Receving call...");
      TelephonyManager telephony = (TelephonyManager) 
      context.getSystemService(Context.TELEPHONY_SERVICE);  
      try {

           Class c = Class.forName(telephony.getClass().getName());
           Method m = c.getDeclaredMethod("getITelephony");
           m.setAccessible(true);
           ITelephony telephonyService = (ITelephony) m.invoke(telephony);

            Bundle b = intent.getExtras();
            String incommingNumber = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

            telephonyService.endCall();

    }catch (Exception e){  e.printStackTrace(); 
}}

Upvotes: 4

Views: 3227

Answers (1)

lopez.mikhael
lopez.mikhael

Reputation: 10081

It is Mission Impossible for the time being. Viewing the reason here : Why it is impossible to intercept incoming calls on Android

But I suggest you still a temporary solution I myself adopted :

Before to block the call, the first thing you need to do is retrieve the current mode of the phone ringing, and then turn on silent :

AudioManager audiomanage = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
int ringerMode = audiomanage.getRingerMode();
audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT);

After blocking the call (or not) gives you the initial mode :

audiomanage.setRingerMode(ringerMode);

You'll need these permissions :

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>

I hope I helped you.

Upvotes: 3

Related Questions