1

I am trying to block incoming call for a particular number. first I try for all incoming call. it works well. but when I try for a particular number by the following code the call is not rejected and the phone is ringing ,I tried with my android phone..

       @Override
     public void onReceive(Context context, Intent intent) {
      Log.v(TAG, "Receving....");
      String t1="01741297163",t2="08801741297163";
      TelephonyManager telephony = (TelephonyManager) 
      context.getSystemService(Context.TELEPHONY_SERVICE);  
      try {
       Class c = Class.forName(telephony.getClass().getName());
       Method m = c.getDeclaredMethod("getITelephony");
       m.setAccessible(true);
       Bundle b=intent.getExtras();
       num=b.getString(telephony.EXTRA_INCOMING_NUMBER);
       if(num.equals(t1)||num.equals(t2)){
       telephonyService = (ITelephony) m.invoke(telephony);
       //telephonyService.silenceRinger();
       telephonyService.endCall();
      } }catch (Exception e) {
       e.printStackTrace();
      }

I do not know the format of incoming number get from the Telephonymanager.

3 Answers 3

1
import java.lang.reflect.Method;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

import com.android.internal.telephony.ITelephony;

public class PhoneCallStateListener extends PhoneStateListener {    

private Context context;
public PhoneCallStateListener(Context context){
    this.context = context;
}


@Override
public void onCallStateChanged(int state, String incomingNumber) {  
SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(context);

switch (state) {

    case TelephonyManager.CALL_STATE_RINGING:       

      String block_number = prefs.getString("block_number", null);
            AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 
            //Turn ON the mute
    audioManager.setStreamMute(AudioManager.STREAM_RING, true);                 
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    try {
    Toast.makeText(context, "in"+block_number, Toast.LENGTH_LONG).show();
    Class clazz = Class.forName(telephonyManager.getClass().getName());
    Method method = clazz.getDeclaredMethod("getITelephony");
    method.setAccessible(true);
    ITelephony telephonyService = (ITelephony) method.invoke(telephonyManager);     
    //Checking incoming call number
    System.out.println("Call "+block_number);
    if (incomingNumber.equalsIgnoreCase("+91"+block_number)) {
    //telephonyService.silenceRinger();//Security exception problem
     telephonyService = (ITelephony) method.invoke(telephonyManager);
     telephonyService.silenceRinger();
    System.out.println(" in  "+block_number);
    telephonyService.endCall();
    }
    } catch (Exception e) {
    Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
    }
        //Turn OFF the mute     
    audioManager.setStreamMute(AudioManager.STREAM_RING, false);
    break;
    case PhoneStateListener.LISTEN_CALL_STATE:
    }
    super.onCallStateChanged(state, incomingNumber);
}
}
0

Why dont you try printing the incoming phone number in log and then comparing it to the numbers that you have stored? Anyways, the phone numbers that you have stored t1 and t2, do not have std/international codes with them. The correct phone number (if in India) would be

t1="+911741297163"

as Indias coed is +91.

2
  • I try with this "+8801741297163" +88 for country code, but not works, then I take last 11 digit for incoming number and compare without country code, then it works successfully. Why I show the incoming number say please. Jan 17, 2013 at 9:30
  • Put Log.v(TAG, "Receving num="+num); and check what is being printed
    – Antrromet
    Jan 17, 2013 at 11:34
0

You should add the country code too with the number. Say +91 for India.Try it!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.