如何从 BroadCastReceiver 更新 Activity 的 UI [英] How to update the UI of Activity from BroadCastReceiver

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

问题描述

我正在学习 Android 概念 ActivityBroadCastReceiver.我想从 BroadtCastReceiver 更新 Activity 的内容,两者都在不同的 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.javaMyBroadtCastReceiver.java

这是否可以在 Android 中执行此操作?

Is this possible to do this in Android ?

推荐答案

BroadcastReceiver 可以以多种方式使用,但是当涉及到更新 的 UI 组件等特定事项时Activity,在它自己的Java类文件中声明/定义一个BroadcastReceiver没有什么好处.

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.

推理 - BroadcastReceiver 必须对 Activity 有一些先验的知识",以及它需要做什么才能更新 UI.实际上,BroadcastReceiver 绑定到 Activity 本身,将其声明/定义为内部类是有意义的.

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.

另一个重要方面是 Activity 需要处于运行"(即可见)状态,以保证对 UI 组件的操作.在这种情况下,在 onResume() 中注册接收器并在 onPause() 中取消注册将有助于防止出现问题.

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);
        }
    }
}

一些额外的注释...

  1. BroadcastReceiver 的生命周期介于进入和离开 onReceive(...) 之间.一旦它从 onReceive(...) 返回,实例就会保持休眠状态,等待下一次广播.
  2. 与第 1 点直接相关 - BroadcastReceiver 不是为繁重工作"而设计的.基本上 onReceive(...) 方法应该尽可能简单.它调用的任何方法也应该尽可能轻量级……进入,做你的事情,离开然后等待下一次广播.如果更新 UI 需要一些时间(例如,通过重新查询数据库获取大量数据来更新 ListView),请考虑调用异步执行的代码(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 更新 Activity 的 UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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