从广播接收到的活动传递数据 [英] Pass data from broadcast receiver to activity

查看:285
本文介绍了从广播接收到的活动传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有正在侦听到WifiManager.SCAN_RESULTS_AVAILABLE_ACTION的广播接收机。
在该接收机我过滤所有可用的网络,并与特定的SSID返回网络的列表。我NNED通过该列表返回到我的应用程序在MainActivity。
我知道我可以为我的广播接收器创建一个内部类,但我preFER创建一个单独的类更好的组织。

I have a broadcast receiver which is listening to the WifiManager.SCAN_RESULTS_AVAILABLE_ACTION. In that receiver I filter all the available networks and return a list of networks with a specific SSID. I nned to pass that list back to the MainActivity of my application. I know I can create an inner class for my Broadcast receiver but I prefer to create a separate class for better organization.

我想在我MainActivity类别创建一个静态变量,然后设置该值。
这是一个好的做法呢?

I am thinking in creating a static variable in my mainActivity class and then set that value. Is this a good practice?

推荐答案

共享和访问信息的一个好办法活性及其他类的十字架是通过使用应用程序对象。只要你有应用程序上下文,你可以访问所有的类的应用对象。

A good way of sharing and access information a cross of Activites and other classes is by using the application object. You can access the application object from all your classes as long as you have the application context.

请参阅本教程的适用对象:的如何使用应用程序对象

See this tutorial about application object: How to use application object

从活动用法:

MyApplicationObject应用=(MyApplicationOjbject)getApplicationContext();

MyApplicationObject app = (MyApplicationOjbject)getApplicationContext();

app.setMyVariable(可变);

app.setMyVariable(variable);

从其他类以外的活动

MyApplicationObject app = (MyApplicationOjbject)context.getApplicationContext();
app.setMyVariable(variable);

这篇关于从广播接收到的活动传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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