onCallStateChanged在onRecieve中不起作用

时间:2018-10-07 12:05:30

标签: android broadcastreceiver

我想阻止我的应用程序用户的所有呼入/呼出电话,但已定义的白名单除外。我已经定义了一个接收器。问题是,当用户第一次拨打自己可以拨打的电话并且白名单中不存在该电话号码时,该电话已被阻止。它应该始终阻止黑名单呼叫。以下是清单中我的接收者。此代码可以很好地处理来电并阻止黑名单号码。我不知道原因:

  public void onReceive(final Context context, Intent intent) {

    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

    this.context = context;
    if(!intent.getAction().equals("android.intent.action.PHONE_STATE")){
        return;
    }
    telephony = (TelephonyManager) context.getApplicationContext()
            .getSystemService(Context.TELEPHONY_SERVICE);
    try {
        Class c = Class.forName(telephony.getClass().getName());
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        telephonyService = (ITelephony) m.invoke(telephony);

    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        databaseHelper=OpenHelperManager.getHelper(context, DatabaseHelper.class);
        connectionRestrictions = databaseHelper.getConnectionRestrictionDao();
    } catch (SQLException ex) {

    }
    telephony.listen(new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {


            if(!incomingNumber.equals("") && incomingNumber != null ){
                switch (state) {
                    case TelephonyManager.CALL_STATE_RINGING:
                        if ((incomingNumber != null)) {
                            try {
                                inComingCallAuthorization(((ParentalControl)context.getApplicationContext()).makeCheckPhoneNumber(incomingNumber));
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                        break;
                    case TelephonyManager.CALL_STATE_IDLE:
                        // your logic here

                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:
                        try {
                            outGoinCallAuthorization(((ParentalControl)context.getApplicationContext()).makeCheckPhoneNumber(incomingNumber));
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        } catch (Exception e){
                            e.printStackTrace();
                        }


                        break;
                    default:

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

        }
    }, PhoneStateListener.LISTEN_CALL_STATE);

}

清单:

      <receiver
            android:name=".reciver.CallServiceReciver">
            <intent-filter android:priority="2147483647">
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>

0 个答案:

没有答案