1

这可能是一个非常简单的问题,但我似乎从一天开始就挂断了这些问题。所以这是一个问题:

如何在从 AndroidManifest 文件注册的 Activity 中取消注册 BroadcastReceiver?这是代码::

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

我熟悉这个功能

public void unregisterReceiver (BroadcastReceiver 接收器)

但如果未从该 Activity 注册,则会引发异常

IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.PHONE_STATE");
registerReceiver(mybroadcast, filter);
4

2 回答 2

4

要禁用receiver中定义的a manifest,请使用以下命令:

PackageManager pm = context.getPackageManager();
ComponentName component = new ComponentName(context, PhoneCallReceiver.class)
pm.setComponentEnabledSetting(component , PackageManager.COMPONENT_ENABLED_STATE_DISABLED , PackageManager.DONT_KILL_APP);
于 2013-02-11T06:17:49.723 回答
0

试试这个:

public void onResume()
  {
    IntentFilter filter = new IntentFilter();
    filter.addAction("android.intent.action.PHONE_STATE");
    registerReceiver(mybroadcast, filter);  

  }

  public void onPause()
  {
    unregisterReceiver(mybroadcast);
  }
于 2013-02-11T06:28:35.997 回答