Android从子线程广播
我正在尝试使用广播器将消息从子线程发送到主 UI 线程。因此,我的活动有一个广播接收器(我讨厌多个),并且我希望能够从一个子线程(可运行)向它们发送消息。
这是我在子线程中所做的事情:
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(ResponseReceiver.ACTION_RESP);
broadcastIntent.putExtra("Controller", "connect");
context.sendBroadcast(broadcastIntent);
但问题是我如何定义上下文?它给出了异常“上下文无法解析”如果我不使用它,它将找不到“sendBroadcast”方法。那么如何才能做到这一点呢?
I'm trying to use broadcasters to send messages from a child thread to the main UI thread. So i have a broadcast receiver on my activities (i hate multiple) and i want to be able to send them messages from one single child thread (runnable).
Here is what i'm doing in the child thread:
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(ResponseReceiver.ACTION_RESP);
broadcastIntent.putExtra("Controller", "connect");
context.sendBroadcast(broadcastIntent);
But the problem is how i define context? It gives the exception "context cannot be resolved" If i don't use it, it won't find "sendBroadcast" method. So how can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将“活动”或“应用程序”上下文传递给线程。
如果您在活动中,您可以执行以下操作:
或
然后将上下文传递给您的子线程:
You can pass in your Activity or Application context to your thread.
If you're in an Activity, you can do:
or
Then pass in context to your child thread: