如何从应用程序 Class 中注销广播接收器?

How to unregister broadcast receiver from an Application Class?

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

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);
   }
}

您应该在 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);
  }
}

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

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

    @Override
    protected void onPause() {
        super.onPause();
        //unregisterReceiver here
    }
}

您的启动器中有一个建议 activity 或需要 activity 像这样取消注册:

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

并在MyApplication中实现unregisterReceiver方法 class:

public void unregisterReceiver() {
     unregisterReceiver(br);//your broadcast
}

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

Benefits to use 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();
}

尝试使用以下代码:
如果您是从 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();
}