2

我在 Android 的应用程序类中注册了广播接收器,但现在我的问题是在哪里取消注册该广播?

public class MyApplication extends Application 

{

@Override
public void onCreate() {
     ..... 
     // OTHER INITIALIZATIONS
     initNetworkBR();
} 
private void initNetworkBR() {

      br = new BroadcastReceiver() {
        ....
      };
      IntentFilter filter = new IntentFilter();
      filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
      registerReceiver(br, filter);
   }
}
4

5 回答 5

3

一个建议是在您的启动器活动或所需的活动中取消注册,如下所示:

@Override
protected void onDestroy() {
    super.onDestroy();
    ((MyApplication) getApplication()).unregisterReceiver();
}

并在类中实现unregisterReceiver方法MyApplication

public void unregisterReceiver() {
     unregisterReceiver(br);//your broadcast
}
于 2017-11-23T12:04:53.520 回答
1

除了在应用程序类中使用广播接收器外,您可以为所有活动使用基类。并在 Base Activity 中注册 BR,然后向孩子提供回调。由于 Activity 具有严格的生命周期模型,因此最好在 Activity 中使用 Runtime Broadcast 接收器而不是 Application 类。

 public class BaseActivity extends AppCompatActivity {
    @Override
    protected void onResume() {
        super.onResume();
        //registerReceiver Here
    }

    @Override
    protected void onPause() {
        super.onPause();
        //unregisterReceiver here
    }
}
于 2017-11-23T12:04:34.520 回答
0

您应该在 onPause() 中使用 unregisterReceiver(BroadcastReceiver receiver) 来取消注册广播接收器。

例子:

public class MyActivity extends Activity {
  private final BroadcastReceiver mybroadcast = new SmsBR();

  public void onResume() {
    super.onResume();

    IntentFilter filter = new IntentFilter();
    filter.addAction("android.provider.Telephony.SMS_RECEIVED");
    registerReceiver(mybroadcast, filter);
  }

  public void onPause() {
    super.onPause();

    unregisterReceiver(mybroadcast);
  }
}
于 2017-11-23T12:03:46.963 回答
0

我认为您应该使用LocalBroadcastManager注册和注销接收器而不是BroadcastReceiver.

使用 LocalBroadcastManager 的好处

@Override
public void onCreate(Bundle savedInstanceState) {

  LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
      new IntentFilter("custom-event-name"));
}

@Override
protected void onDestroy() {
  // Unregister since the activity is about to be closed.
  LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
  super.onDestroy();
}
于 2017-11-23T12:10:15.043 回答
0

尝试使用以下代码:
如果您已从 Manifiest.xml 注册

ComponentName component = new ComponentName(context, MyReceiver.class);

    int status = context.getPackageManager().getComponentEnabledSetting(component);
    if(status == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
     context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED , PackageManager.DONT_KILL_APP);
    }

如果以编程方式注册接收者:

@Override
public void onCreate(Bundle savedInstanceState) {

  LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
      new IntentFilter("custom-event-name"));
}

@Override
protected void onDestroy() {
  // Unregister since the activity is about to be closed.
  LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
  super.onDestroy();
}
于 2017-11-23T12:19:51.433 回答