活动和广播接收器之间的android沟通 [英] android communicate between activity and broadcast receiver

查看:101
本文介绍了活动和广播接收器之间的android沟通的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有显示从服务器获取一些数据的活动。如果没有连接可用,活动显示一些缓存的数据;如果连接是可用的,活动获取数据并显示。它按预期工作的。
现在,我想提出我的活动,一旦连接出现重新加载数据。我使用一个简单的接收器,它扩展了BroadcastReceiver的:

I have an activity which displays some data fetched from the server. If no connection is available, activity displays some cached data; if connection is available, activity fetches data and displays it. It all works as expected.
Now, I would like to make my activity reload the data as soon as the connection occurs. I am using a simple Receiver that extends the BroadcastReceiver:

public class ConnectionChangeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetInfo != null) {
            //what to do here? 
        } 
     }
}

广播接收装置,在我的清单文件中声明如下:

Broadcast receiver is declared in my manifest file as follows:

<receiver android:name=".ConnectionChangeReceiver"
          android:label="NetworkConnection">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
        </intent-filter>
</receiver>

在我的活动,我注册了接收器:

In my activity, I register the receiver:

ConnectionChangeReceiver接收器=新ConnectionChangeReceiver();   this.registerReceiver(接收器,                 新的IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

ConnectionChangeReceiver receiver = new ConnectionChangeReceiver(); this.registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

现在,我很困惑,下一步该怎么做。当执行的onReceive方法,如何让我的活动意识到这一点?我知道我可以开始一个新的活动,但是这并不是我真正想要的。我应该声明ConnectionChangeReceiver作为私有类我的活动?或者有没有其他的解决办法吗?

Now, I am confused as what to do next. When onReceive method is executed, how to make my activity aware of that? I know I could start a new activity, but that's not really what I want. Should I declare ConnectionChangeReceiver as a private class of my activity? Or is there any other solution?

推荐答案

我觉得建立接收器的活动的专用子类是要走的路在这里。通过这种方式,你可以控制你的活动事件和数据。然后,你可以创建它的一个实例,从code如同上面一样注册的接收器。

I think building the receiver as a private subclass of your activity is the way to go here. This way you can control events and data from your activity. Then you can just create one instance of it and register the receiver from code as you did above.

请注意,您不必注册您的接收器在两个清单和code。其中之一是enugh - 清单基本上是一个静态的登记,而这样做的code允许动态注册在运行时。此外,当你在清单中注册,你的接收机的一个新实例将自动从系统中,执行和终止创建。做了reg在code允许指向一个特定的实例。

Note that you don't have to register your receiver in both the manifest and code. One of these is enugh - the manifest is basically a "static" registration while doing it in code allows dynamic registration at runtime. Also when you register in the manifest, a new instance of your receiver will automatically be created from the system, executed and terminated. Doing the reg in code allows to point to one specific instance.

这篇关于活动和广播接收器之间的android沟通的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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