无法禁用广播接收器

时间:2013-05-13 03:44:20

标签: android broadcastreceiver

我从here获得了禁用/启用广播接收器的逻辑。

@Override
    public void onCheckedChanged(CompoundButton v, boolean checked) {
        // TODO Auto-generated method stub

        if (v == disableBlock) {
            manageBlockSetting(checked);
        }
    }

private void manageBlockSetting(boolean disable) {
        Log.e(tag, "Disable : " + disable);
        int flag = disable ? PackageManager.COMPONENT_ENABLED_STATE_DISABLED
                : PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
        ComponentName component = new ComponentName(SettingActivity.this, PhoneCallReceiver.class);

        getApplication().getPackageManager().setComponentEnabledSetting(component, flag, PackageManager.DONT_KILL_APP);
        editSharedPreferences(StoreConstantValue.SETTING_DISABLE_BLOCK, disable);
}

在Manifest.xml中

<receiver android:name="com.vsmart.unocaller.blockingservice.PhoneCallReceiver" 
            android:enabled="true" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>

部署android:enabled="false"

我可以启用一次。但是当我想要禁用时。什么都没发生,我无法禁用它。它仍然阻止来电。

我的设备是带有Android 2.3.3的三星Galaxy S2

有任何帮助吗?感谢

1 个答案:

答案 0 :(得分:1)

您可以尝试从代码中注册/取消注册接收器,而不是在清单中声明接收器。例如

private class MyPhoneStateListener extends PhoneStateListener {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);

    }
}


private MyPhoneStateListener phoneStateListener = new MyPhoneStateListener();

启用

        telephonyManager.listen(phoneStateListener, LISTEN_CALL_STATE);

并禁用

    telephonyManager.listen(phoneStateListener,
            PhoneStateListener.LISTEN_NONE);

其中

        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);