我的要求是,结束所有来电。我已经用谷歌搜索过了,并浏览了这些link1link2
我的问题是:
从上面的帮助链接我试图阻止呼叫,它虽然是结束来电,但经过一个单一的振动。我不想那个单一的振动,只是想取消电话直接没有任何振动或通知。
这是我的代码:
显示::

 <uses-permission android:name="android.permission.READ_PHONE_STATE" />
 <uses-permission android:name="android.permission.CALL_PHONE" />
 <uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS" />

 <receiver android:name=".PhoneStateBroadcastReceiver" >
    <intent-filter>
      <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
 </receiver>

收件人:
  public class PhoneStateBroadcastReceiver extends BroadcastReceiver {
     public void onReceive(Context context, Intent intent) {
       if ("android.intent.action.PHONE_STATE".equals(str)) {
       Bundle localBundle = intent.getExtras();
       String callState = localBundle.getString("state");
         if (callState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
           try{
                 TelephonyManager localTelephonyManager = (TelephonyManager) context.getSystemService("phone");

                 Method localMethod = Class.forName(localTelephonyManager.getClass()
                .getName()).getDeclaredMethod("getITelephony", new Class[0]);

                 localMethod.setAccessible(true);

                 boolean bool = ((ITelephony)localMethod.invoke(localTelephonyManager,
                 new Object[0])).endCall();

                 return;
           }catch (Exception localException){
                localException.printStackTrace();
           }
         }
       }
      }
    }

在“com.android.internal.telephony”包下创建这个接口“itelephony.java”
itelephony接口:
   public abstract interface ITelephony extends IInterface{
     public abstract void answerRingingCall() throws RemoteException;
     public abstract boolean endCall() throws RemoteException;
     public abstract void silenceRinger() throws RemoteException;
     public static abstract class Stub extends Binder implements ITelephony{

      private static final String DESCRIPTOR = "com.android.internal.telephony.ITelephony";
      static final int TRANSACTION_answerRingingCall = 2;
      static final int TRANSACTION_endCall = 1;
      static final int TRANSACTION_silenceRinger = 3;
      public Stub(){
         attachInterface(this, "com.android.internal.telephony.ITelephony");
      }
      public static ITelephony asInterface(IBinder paramIBinder){
         if (paramIBinder == null)
             return null;
         IInterface localIInterface = paramIBinder.
         queryLocalInterface("com.android.internal.telephony.ITelephony");

         if ((localIInterface != null) && ((localIInterface instanceof ITelephony)))
         return (ITelephony)localIInterface;
         return new Proxy(paramIBinder);
      }
     public IBinder asBinder(){
         return this;
     }
     public boolean onTransact(int paramInt1, Parcel paramParcel1,
      Parcel paramParcel2, int paramInt2)throws RemoteException {

      switch (paramInt1){
       default:
         return super.onTransact(paramInt1, paramParcel1, paramParcel2, paramInt2);
       case 1598968902:
         paramParcel2.writeString("com.android.internal.telephony.ITelephony");
         return true;
       case 1:
         paramParcel1.enforceInterface("com.android.internal.telephony.ITelephony");
         boolean bool = endCall();
         paramParcel2.writeNoException();
         if (bool);
     for (int i = 1; ; i = 0)
    {
      paramParcel2.writeInt(i);
      return true;
    }
  case 2:
    paramParcel1.enforceInterface("com.android.internal.telephony.ITelephony");
    answerRingingCall();
    paramParcel2.writeNoException();
    return true;
  case 3:
  }

最佳答案

实际上,您已经编写了一个类似phoneCallStateListener的类,它实现了phoneStateListener并使用它的回调方法。

public class PhoneCallStateListener extends PhoneStateListener {

    private Context context;
    private SharedPreferences prefs;
    private ITelephony telephonyService;
    // I add a block number manually.
    private String block_number = "01912067599";
    TelephonyManager telephonyManager;

    public PhoneCallStateListener(Context context) {
        this.context = context;
        prefs = PreferenceManager.getDefaultSharedPreferences(context);
        telephonyManager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
    }

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {

        switch (state) {

        case TelephonyManager.CALL_STATE_RINGING:

            Toast.makeText(context,
                    "<<-- CALL_STATE_RINGING -->> \n " + incomingNumber,
                    Toast.LENGTH_SHORT).show();

            endThecall(incomingNumber);

            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:

            // blockAllOutgoingNumber(incomingNumber);

            Toast.makeText(context, "CALL_STATE_OFFHOOK", Toast.LENGTH_SHORT)
                    .show();
            break;

        case TelephonyManager.CALL_STATE_IDLE:
            Toast.makeText(context, "CALL_STATE_IDLE", Toast.LENGTH_SHORT)
                    .show();
            break;

        }
        // super.onCallStateChanged(state, incomingNumber);
    }

    private void blockAllOutgoingNumber(String outGoingNumber) {

        // Class clazz;
        try {
            Class clazz = Class.forName(telephonyManager.getClass().getName());
            Method method = clazz.getDeclaredMethod("getITelephony");
            method.setAccessible(true);

            telephonyService = (ITelephony) method.invoke(telephonyManager);
            // telephonyService.silenceRinger();
            System.out.println(" block this outGoingNumber! wao."
                    + outGoingNumber);
            telephonyService.endCall();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /**
     * @param incomingNumber
     */
    private void endThecall(String incomingNumber) {

        // String block_number = prefs.getString("block_number", null);
        AudioManager audioManager = (AudioManager) context
                .getSystemService(Context.AUDIO_SERVICE);
// check current phone state, do what u want to do
// :) :D ;)
   switch (audioManager.getRingerMode()) {
    case AudioManager.RINGER_MODE_NORMAL:

        break;
    case AudioManager.RINGER_MODE_SILENT:

        break;
    case AudioManager.RINGER_MODE_VIBRATE:

        // To Enable silent mode.....
        audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

        // To Enable Ringer mode.....
        // audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

        break;
    }
        try {

            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.equals("+88" + block_number)) {
                telephonyService = (ITelephony) method.invoke(telephonyManager);
                // telephonyService.silenceRinger();
                System.out.println(" block this number! wao." + block_number);

                // Turn ON the mute
                audioManager.setStreamMute(AudioManager.STREAM_RING, true);
                telephonyService.endCall();
                // to handle vibration write code here
                // it will be easy!!


            } else {
                // Turn Off the mute
                audioManager.setStreamMute(AudioManager.STREAM_RING, false);

            }

        } catch (Exception e) {
            Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
        }

    }
}

在初始化PhoneCallStateListener类之后,在类内部输入CallReceiver,它像下面这样扩展BroadcastReceiver。
    public class IncommingCallReceiver extends BroadcastReceiver {

    Context context = null;
    private static final String TAG = "Phone call";
    private ITelephony telephonyService;

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

        TelephonyManager telephony = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);

        PhoneCallStateListener customPhoneListener = new PhoneCallStateListener(
                context);
        telephony.listen(customPhoneListener,
                PhoneStateListener.LISTEN_CALL_STATE);



    }

}

清单里面写着:>>>
        <receiver android:name="IncommingCallReceiver" >
        <intent-filter android:priority="100" >
            <action android:name="android.intent.action.PHONE_STATE" >
            </action>
        </intent-filter>
    </receiver>

显然,您必须在“com.android.internal.telephony”包下创建itelephony.java接口,但对于我的情况:
iTelephony界面外观:>>>
 package com.android.internal.telephony;

  interface ITelephony {


    boolean endCall();


    void answerRingingCall();


    void silenceRinger();

  }

10-07 13:21