如何更新活动的BroadcastReceiver从用户界面 [英] How to update the UI of Activity from BroadCastReceiver

查看:124
本文介绍了如何更新活动的BroadcastReceiver从用户界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习的Andr​​oid 概念活动广播接收器 。我想更新活动的内容 BroadtCastReceiver 都是在不同的Java类。

I am learning Android concepts Activity and BroadCastReceiver. I want to update the content of Activity from the BroadtCastReceiver both are in different java class.

这是一样的东西。

MyActivity.java MyBroadtCastReceiver.java

这是可能做到这一点在Android中?

Is this possible to do this in Android ?

推荐答案

A 广播接收器可以通过多种方式来使用,但是当涉及到​​一些具体的更新UI一个活动的成分,很少有优势声明/定义广播接收器在它自己的Java类文件。

A BroadcastReceiver can be used in many ways but when it comes to something as specific as updating the UI components of an Activity, there is little advantage to declaring / defining a BroadcastReceiver in it's own Java class file.

推理 - 对广播接收器必须具有活动和它所需要的一些事先知识做的,以更新UI。生效的广播接收器是联系在一起的活动本身是有意义的声明/把它定义为一个内部类。

Reasoning - the BroadcastReceiver has to have some prior "knowledge" of the Activity and what it is required to do in order to update the UI. In effect the BroadcastReceiver is tied to the Activity itself and it makes sense to declare / define it as an inner class.

另一个重要的方面是活动需要在跑(即,可见)状态,以保证UI组件的操作。在这种情况下,在 onResume注册接收机()的onPause注销()将帮助prevent问题

Another important aspect is the Activity needs to be in a "running" (i.e., visible) state in order to guarantee manipulation of UI components. In this case, registering the receiver in onResume() and unregistering in onPause() will help prevent problems.

使用通用模板我会做类似如下...

Using a generic template I'd do something like the following...

class MyActivity extends Activity {

    boolean mIsReceiverRegistered = false;
    MyBroadcastReceiver mReceiver = null;

    // onCreate(...) here

    @Override
    protected void onResume() {

        // Other onResume() code here

        if (!mIsReceiverRegistered) {
            if (mReceiver == null)
                mReceiver = new MyBroadcastReceiver();
            registerReceiver(mReceiver, new IntentFilter("YourIntentAction"));
            mIsReceiverRegistered = true;
        }
    }

    @Override    
    protected void onPause() {
        if (mIsReceiverRegistered) {
            unregisterReceiver(mReceiver);
            mReceiver = null;
            mIsReceiverRegistered = false;
        }

        // Other onPause() code here

    }

    private void updateUI(Intent intent) {
        // Do what you need to do
    }

    private class MyBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            updateUI(intent);
        }
    }
}

编辑:一对夫妇的附加说明...

A couple of extra notes...


  1. 的生命周期广播接收器是进入和退出之间的的onReceive(...)。一旦它已经从的onReceive(...)的实例仍然处于休眠状态等待下一次的广播。
  2. 返回
  3. 直接关系到1点 - 一个广播接收器不是专为繁重。基本上的onReceive(...)方法应保持尽可能简单。它调用任何方法也应尽可能重量轻越好......得到的,做你的东西,走出去,然后等待下一次的广播。如果更新用户界面是要花费一些时间(也许是更新的ListView 通过重新查询了大量的例如数据的数据库),考虑调用code它执行异步(一个的AsyncTask 为例)。

  1. The life-cycle of a BroadcastReceiver is between entering and leaving onReceive(...). Once it has returned from onReceive(...) the instance remains in a dormant state waiting for the next broadcast.
  2. Directly related to point 1 - a BroadcastReceiver isn't designed for "heavy lifting". Basically the onReceive(...) method should be kept as simple as possible. Any methods it calls should also be as light-weight as possible...get in, do your stuff, get out then wait for the next broadcast. If updating the UI is going to take some time (perhaps updating a ListView by re-querying a database for a large amount of data for example), consider calling code which performs asynchronously (an AsyncTask for example).

这篇关于如何更新活动的BroadcastReceiver从用户界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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