Android阻止传出呼叫问题

时间:2015-05-26 11:15:08

标签: android android-broadcast

我想根据某些逻辑阻止去电话。 我使用下面的代码

public class OutgoingCallReceiver extends BroadcastReceiver {

    private OutgoingCallListener customPhoneListener = null;
    public static boolean check = true;

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

            String originalNumber = intent
                    .getStringExtra(Intent.EXTRA_PHONE_NUMBER);

            TelephonyManager telephony = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            customPhoneListener = new OutgoingCallListener(context,
                    originalNumber);
            telephony.listen(customPhoneListener,
                    PhoneStateListener.LISTEN_CALL_STATE);

        }

    }

}



  // Blocking call
            public void blockCall(Context context) {
                try {
                    TelephonyManager tm = (TelephonyManager) context
                            .getSystemService(Context.TELEPHONY_SERVICE);
                    Class<?> c = Class.forName(tm.getClass().getName());
                    Method m = c.getDeclaredMethod("getITelephony");
                    m.setAccessible(true);
                    ITelephony telephonyService = (ITelephony) m.invoke(tm);
                    telephonyService = (ITelephony) m.invoke(tm);
                    // telephonyService.silenceRinger();

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



<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<receiver android:name="com.demo.example.OutgoingCallReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </receiver>

问题是::当我从手机拨打电话时。 Onreceive多次呼叫,并给出不同的数字。

请帮帮我。谢谢。

1 个答案:

答案 0 :(得分:1)

我也面临同样的问题。这是因为使用。

这里首先将您的号码存储在某个对象中。请参阅下面的代码。

   <receiver android:name="com.perumaltt.cloud.receiver.ProcessCall" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </receiver>



public class ProcessCall extends BroadcastReceiver {

    public static String number;
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        String originalNumber = intent
                .getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        System.out.println("originalNumber :::: " + originalNumber);
        if (originalNumber != null) {
            number = originalNumber;
        }


        TelephonyManager telephony = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        MyPhoneStateListener listener = new MyPhoneStateListener(context,originalNumber);
        telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
    }

}




// My listener
    public class MyPhoneStateListener extends PhoneStateListener {

        Context context;
        String outGoingCall;

        public MyPhoneStateListener(Context context, String outGoingCall) {
            super();
            this.context = context;
            this.outGoingCall = outGoingCall;
        }

        @Override
        public void onCallStateChanged(int state, String callingNumber) {
            System.out.println("callingNumber MyPhoneStateListener before"
                    + callingNumber);
            // Log.i("callingNumber :::: MyPhoneStateListener ", outGoingCall);
    //      if(callingNumber == null || callingNumber.trim().length() < 3){
    //          return;
    //      }
    //      if(outGoingCall == null || outGoingCall.trim().length() < 3){
    //          return;
    //      }
            System.out.println("callingNumber MyPhoneStateListener after"
                    + callingNumber);
            System.out.println("state ::: "+state);

            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                break;

            case TelephonyManager.CALL_STATE_OFFHOOK:
                // handle out going call
                callOut(outGoingCall);
                break;

            case TelephonyManager.CALL_STATE_RINGING:
                // handle in coming call
                endCallIfBlocked(callingNumber);
                break;

            default:
                break;
            }
            super.onCallStateChanged(state, callingNumber);
        }

        synchronized private void callOut(String outGoingCall1) {
            if(outGoingCall1 == null){
                return;
            }
            if(ProcessCall.number.equalsIgnoreCase(outGoingCall1)){
                System.out.println("yes same ******** ");
            }else {
                System.out.println("not same ******* ");
                return;
            }

            System.out.println("call out :::: "+outGoingCall1);
            if (UserDetials.getInstance().getCallController().ismCallOutList()
                    && outGoingCall != null) {
                ArrayList<ContactsModel> numbers = CloudApplication.database
                        .getContacts(MySQLiteHelper.COLUMN_PHONE_NUMBER,
                                outGoingCall);
                if (numbers.size() > 0) {
                    ContactsModel model = numbers.get(0);
                    System.out.println("type :::: " + model.getType());
                    if (model.getType() != Constants.CALL_OUT) {
                        CommonMethods.getInstance().blockCall(context,outGoingCall1,Constants.CALL_OUT);
                    }
                }else{
                    CommonMethods.getInstance().blockCall(context,outGoingCall1,Constants.CALL_OUT);
                }

            }
        }

        private void endCallIfBlocked(String callingNumber) {

            System.out.println("endCallIfBlocked :::: endCallIfBlocked");

            ArrayList<ContactsModel> numbers = CloudApplication.database
                    .getContacts(MySQLiteHelper.COLUMN_PHONE_NUMBER, callingNumber);

            if (UserDetials.getInstance().getCallController().ismCallInList()) {

                if (numbers.size() > 0) {
                    ContactsModel model = numbers.get(0);
                    if (model.getType() != Constants.CALL_IN) {
                        CommonMethods.getInstance().blockCall(context,callingNumber,Constants.CALL_IN);
                    }
                } else {
                    CommonMethods.getInstance().blockCall(context,callingNumber,Constants.CALL_IN);
                }

            } else if (UserDetials.getInstance().getCallController().ismBlockList()) {

                if (numbers.size() > 0) {
                    ContactsModel model = numbers.get(0);
                    if (model.getType() == Constants.BLOCK_LIST) {
                        CommonMethods.getInstance().blockCall(context,callingNumber,Constants.BLOCK_LIST);
                    }

                }

            }

        }

    }


// Blocking code
 public void blockCall(Context context) {
                try {
                    TelephonyManager tm = (TelephonyManager) context
                            .getSystemService(Context.TELEPHONY_SERVICE);
                    Class<?> c = Class.forName(tm.getClass().getName());
                    Method m = c.getDeclaredMethod("getITelephony");
                    m.setAccessible(true);
                    ITelephony telephonyService = (ITelephony) m.invoke(tm);
                    telephonyService = (ITelephony) m.invoke(tm);

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

就是这样。享受