凡注册一个BroadcastReceiver(活动周期的乐趣) [英] Where to register a BroadcastReceiver (Activity lifecycle fun)

查看:102
本文介绍了凡注册一个BroadcastReceiver(活动周期的乐趣)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在哪里注册/在活动注销意图接收正确的地方?通常我会把这样的东西在这里:

Where's the right place to register/unregister an intent receiver in an Activity? Usually I would put stuff like this here:

class MyActivity 
{
    private BroadcastReceiver mMyReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.v(TAG, "Do something.");
        }
    };

    @Override
    public void onResume() {
        super.onResume();

        registerReceiver(mMyReceiver, new IntentFilter(...));
    }

    @Override
    public void onPause() {
        super.onPause();

        unregisterReceiver(mMyReceiver);
    }
}

问题是,我的活动就可以不再对广播如果它发生,而它在暂停状态作出反应。哪里是做这样的事情在正确的地方呢?

The problem is that my activity can then no longer respond to the broadcast if it happens while it's in the pause state. Where's the right place to do something like this then?

感谢

推荐答案

这应该在你的情况下工作:

This should work in your case:

Unregister in onDestroy(). 

这篇关于凡注册一个BroadcastReceiver(活动周期的乐趣)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆