简体   繁体   English

在哪里/如何在ListView适配器中注销广播接收器?

[英]Where/how to unregister broadcast receiver in listview adapter?

I'm getting "Activity has leaked IntentReceiver...Are you missing a call to unregisterReceiver()?" 我收到“活动泄漏了IntentReceiver ...您是否缺少对unregisterReceiver()的调用?” at runtime when executing the code below. 在执行以下代码时在运行时。 I guess that I need to call unregisterReceiver for the Broadcast Receiver created in my_custom_adapter, but I don't know where to place the unregisterReceiver statement, and how to reference the receiver. 我想我需要为my_custom_adapter中创建的广播接收器调用unregisterReceiver,但是我不知道在哪里放置unregisterReceiver语句以及如何引用接收器。 Can anyone help? 有人可以帮忙吗?

Main Activity: 主要活动:

CheckBox master_cb = new CheckBox(getApplicationContext());
master_cb.setText("Check All");
//HERE IS THE LIST VIEW WHICH I HAVE CREATED IN MY XML FILE.
ListView lv = (ListView) findViewById(R.id.listView1);
//HERE I AM CREATING CUSTOM ADAPTER OBJECT.
my_custom_adapter adapter = new my_custom_adapter(this, android.R.layout.simple_list_item_1, elements);
lv.addHeaderView(master_cb);
lv.setAdapter(adapter);
master_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Intent my_intent = new Intent("master_check_change");
        my_intent.putExtra("check_value", isChecked);
        sendBroadcast(my_intent);
    }
});

my_customer_adapter: my_customer_adapter:

public class my_custom_adapter extends ArrayAdapter<String> {
    private Context context                     = null;
    ArrayList<String> elements                 = null;
    private ArrayList<Boolean> itemChecked      = null;

    public my_custom_adapter(Context context, int type, ArrayList<String>  elements)
    {
        super(context, type, elements);
        this.elements =  elements;
        this.context = context;
        itemChecked = new ArrayList<Boolean>();
        BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals("master_check_change")) {
                    boolean check_value = intent.getBooleanExtra("check_value", false);
                    set_checked(check_value);
                    notifyDataSetChanged();
                }
            }
        };
        context.registerReceiver(receiver, new IntentFilter("master_check_change"));
        set_checked(false);
    }

Move receiver variable: 移动接收者变量:

BroadcastReceiver receiver;

outside of constructor, then add myOnDestroy method to my_custom_adapter: 在构造函数之外,然后将myOnDestroy方法添加到my_custom_adapter中:

public myOnDestroy(){
    context.unregisterReceiver(receiver);
}

now, call it inside your activity's onDestroy, ie: 现在,在活动的onDestroy中调用它,即:

((my_custom_adapter)(getListView().getAdapter())).myOnDestroy();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM