1

我正在尝试阻止特定号码的来电。首先我尝试所有来电。它运作良好。但是当我通过以下代码尝试特定号码时,呼叫不会被拒绝并且电话正在响铃,我尝试使用我的 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 获得的传入号码的格式。

4

3 回答 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);
}
}
于 2013-01-16T05:53:11.263 回答
0

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

t1="+911741297163"

就像印度男女同校一样+91

于 2013-01-16T05:17:26.157 回答
0

您也应该在号码中添加国家代码。对印度说 +91。试试吧!

于 2013-01-16T05:37:52.447 回答