招致拦截来电的问题

时间:2014-03-22 13:14:49

标签: android phone-call

我的要求是,结束所有来电。我用谷歌搜索了这些link1link2

我的问题是::

从上面链接的帮助我试图阻止呼叫,它虽然结束来电,但是在一次振动之后。我不想要单一振动,只是想直接取消呼叫,没有任何振动或通知。

这是我的代码::

Manifest ::

 <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:
  }

1 个答案:

答案 0 :(得分:1)

实际上你已经编写了一个像 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类之后,在您的类 IncommingCallReceiver 中扩展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);



    }

}

Inside manifest中写道:&gt;&gt;&gt;

        <receiver android:name="IncommingCallReceiver" >
        <intent-filter android:priority="100" >
            <action android:name="android.intent.action.PHONE_STATE" >
            </action>
        </intent-filter>
    </receiver>

显然你必须在&#34; com.android.internal.telephony &#34;包下创建这个界面 ITelephony.java 。但对于我的情况:

ITelephony 界面看起来:&gt;&gt;&gt;

 package com.android.internal.telephony;

  interface ITelephony {


    boolean endCall();


    void answerRingingCall();


    void silenceRinger();

  }