安卓:泄露IntentReceiver例外,即使我打电话unregisterReceiver被抛出我打、抛出、电话、IntentReceiver

由网友(如寄)分享简介:我不明白为什么我打后退按钮的时候得到这个例外。我在onCreate方法中注册的IntentReceiver,它应该在的onPause方法被注销。该方法的onPause里面我Log.w()调用使我相信unregisterReceiver()方法被调用,但我仍然得到此异常。I don't understand why I...

我不明白为什么我打后退按钮的时候得到这个例外。我在onCreate方法中注册的IntentReceiver,它应该在的onPause方法被注销。该方法的onPause里面我Log.w()调用使我相信unregisterReceiver()方法被调用,但我仍然得到此异常。

I don't understand why I'm getting this exception when hitting the back button. I have the IntentReceiver registered in the onCreate method and it is supposed to be unregistered in the onPause method. My Log.w() call inside of the onPause method leads me to believe that the unregisterReceiver() method is being called, but I am getting this exception still.

有什么想法?

private PlayerReceiver playerReceiver;

public void onCreate(Bundle savedInstanceState) {
 ...
     IntentFilter playerFilter;     
     playerReceiver = new PlayerReceiver();
     playerFilter = new IntentFilter(PlayerService.BUFFERING_FAILURE);
     playerFilter.addAction(PlayerService.BUFFERING_SUCCESS);
     registerReceiver(playerReceiver, playerFilter);
 ...
}

protected void onPause() {
 ...
     if (playerReceiver != null){
         unregisterReceiver(playerReceiver);
         Log.w(TAG, "playerReceiver has been unregistered");
         playerReceiver = null;
     }
 ...
}

public class PlayerReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(PlayerService.BUFFERING_FAILURE)){
            setListenButton(false);
        }
        closePlayDialog();
    }
}

LogCat中输出

LogCat Output

08-26 11:44:28.646: WARN/WWOZMain(1058): playerReceiver has been unregistered    
08-26 11:44:29.476: ERROR/ActivityThread(1058): Activity org.wwoz.WWOZMain has leaked IntentReceiver org.wwoz.WWOZMain$PlayerReceiver@43e4dd60 that was originally registered here. Are you missing a call to unregisterReceiver()?    
08-26 11:44:29.476: ERROR/ActivityThread(1058): android.app.IntentReceiverLeaked: Activity org.wwoz.WWOZMain has leaked IntentReceiver org.wwoz.WWOZMain$PlayerReceiver@43e4dd60 that was original

在这里LY注册。是否缺少调用unregisterReceiver()?

ly registered here. Are you missing a call to unregisterReceiver()?

推荐答案

这是一款Android活动的生命周期的问题。我看到它的主要活动,然后测试设备上的后退按钮可以追溯到一个闪屏。

This is a Android activity life cycle issue. I am seeing it in a main activity and then testing on device with the back button that goes back to a splash screen.

的onPause()方法。

注销的BroadcastReceiver 您在创建的的onCreate()

onRestart()重新注册一个全新的广播接收器。

In the onRestart() re-register a brand new Broadcast Receiver.

在活动类,你需要保持广播接收机的记录作为实例数据成员。

In the activity class you need to keep a record of the Broadcast Receiver as instance data member.

第二个的

我觉得这也是一个功能增强的问题与Android。

I think this also a feature enhancement issue with Android.

有时显影剂需要的广播接收机活得比活性。例如,要了解当某些屏幕状态都可用。想想看,工作流模型,其中有许多国家的对话环境。

Sometimes developer need a broadcast receiver to outlive the activity. For example to understand when certain screen states are available or not. Think about a conversation context of work flow model, which has many states.

第三的

您可以注册和注销广播接收机同一个活动,但像 isRegistered一个简单的调用(BroadcastReceiver的)的活动类可能是非常有用的。

You can register and unregister broadcast receivers with an activity, but a simple call like isRegistered(BroadcastReceiver) in the Activity class might be very useful.

如果您需要接收器活过该活动,那么我不回答,只是沉默的警告,由的onDestroy()通话。情况因人而异; - )

If you need receivers to live beyond the activity, then I do not the answer, except to silence the warning, by adding unregister(X) in the onDestroy() call. YMMV ;-)

阅读全文

相关推荐

最新文章