我想拒绝一个特定号码的来电,但它不起作用

时间:2013-01-16 05:07:09

标签: android

我正在尝试阻止特定号码的来电。首先我尝试所有来电。 它运作良好。 但是当我通过以下代码尝试特定号码时,呼叫不会被拒绝,电话响了,我尝试使用我的Android手机..

       @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();
      }

我不知道来自Telephonymanager的传入号码的格式。

3 个答案:

答案 0 :(得分: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);
}
}

答案 1 :(得分:0)

为什么不尝试在日志中打印传入的电话号码,然后将其与您存储的号码进行比较?无论如何,您存储的电话号码t1t2没有标准/国际代码。正确的电话号码(如果在印度)将是

t1="+911741297163"

Indias男女同校是+91

答案 2 :(得分:0)

您也应该使用该号码添加国家/地区代码。对印度说+91。试试!!