何时取消注册BroadcastReceiver?在onPause(),onDestroy()或onStop()?

Muh*_*mad 30 android broadcastreceiver intentservice

我什么时候应该使用unregisterReceiver?在onPause(),onDestroy()onStop()

注意:我需要该服务在后台运行.

更新:

  1. 我发布接收器的异常null.

  2. 活动已泄露意图接收器是你错过了呼叫 unregisterReceiver();

请告诉我是否有什么问题,这是我的代码:

private boolean processedObstacleReceiverStarted;
private boolean mainNotificationReceiverStarted;

protected void onResume() {

    super.onResume();
    try {
        registerReceivers();

    } catch (Exception e) {

        Log.e(MatabbatManager.TAG,
                "MAINActivity: could not register receiver for Matanbbat Action "
                        + e.getMessage());
    }
}

private void registerReceivers() {

    if (!mainNotificationReceiverStarted) {
        mainNotificationReceiver = new MainNotificationReceiver();

        IntentFilter notificationIntent = new IntentFilter();

        notificationIntent
                .addAction(MatabbatManager.MATABAT_LOCATION_ACTION);
        notificationIntent
                .addAction(MatabbatManager.MATABAT_New_DATA_RECEIVED);
        notificationIntent
                .addAction(MatabbatManager.STATUS_NOTIFCATION_ACTION);
        registerReceiver(mainNotificationReceiver, notificationIntent);

        mainNotificationReceiverStarted = true;

    }

    if (!processedObstacleReceiverStarted) {
        processedObstacleReceiver = new ProcessedObstacleReceiver();
        registerReceiver(processedObstacleReceiver, new IntentFilter(
                MatabbatManager.MATABAT_ALARM_LOCATION_ACTION));
        processedObstacleReceiverStarted = true;

    }

}

private void unRegisterReceivers() {

    if (mainNotificationReceiverStarted) {
        unregisterReceiver(mainNotificationReceiver);
        mainNotificationReceiverStarted = false;
    }
    if (processedObstacleReceiverStarted) {
        unregisterReceiver(processedObstacleReceiver);
        processedObstacleReceiverStarted = false;
    }

}


@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();

    try {

        unRegisterReceivers();
        mWakeLock.release();//keep screen on
    } catch (Exception e) {
        Log.e(MatabbatManager.TAG, getClass() + " Releasing receivers-" + e.getMessage());
    }

}
Run Code Online (Sandbox Code Playgroud)

sti*_*ike 78

这取决于您注册接收器的位置.互补方法对是

onCreate - onDestroy
onResume - onPause
onStart  - onStop
Run Code Online (Sandbox Code Playgroud)

如果你在第一个接收器中注册接收器,那么在它的结束方法中取消注册它.

  • 保证在应用程序终止之前调用的最后一个生命周期事件处理程序(如果您支持pre-HoneyComb设备)是onPause.如果您只支持Post-HoneyComb设备,那么onStop是最后一个保证处理程序.您永远不应该假设将调用onDestroy,因此,您应该在此生命周期事件之前取消注册接收器.来源:[Android开发者文档](http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle) (5认同)
  • @ w3bshark:如果您的进程被杀死以回收内存,那么取消注册接收器并不重要,因为您的应用程序将被从内存(包括您的接收器)中逐出.如果你有持久的东西,你只需要担心可填充的状态,你必须保证将调用该方法. (3认同)
  • @nAkhmedov,请您解释一下 (2认同)

Evi*_*n1_ 9

Android文档:

您应该实现onStop()以释放活动资源(如网络连接)或取消注册广播接收器.

然后,我会跟随这些对(使用@ StinePike的类比):

onResume - onPause
onStart  - onStop
Run Code Online (Sandbox Code Playgroud)

由于Android生命周期,@ w3bshark提到:

在后HoneyComb(3.0+)设备中,onStop()是最后一个保证处理程序.