小编Muh*_*mad的帖子

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

我什么时候应该使用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 …
Run Code Online (Sandbox Code Playgroud)

android broadcastreceiver intentservice

30
推荐指数
2
解决办法
2万
查看次数

标签 统计

android ×1

broadcastreceiver ×1

intentservice ×1