将值从活动传递到广播接收器并从广播接收器启动服务
我有一个活动。它包含一个按钮,其文本会动态变化。我想将此文本传递给接收短信的广播接收器。现在我的广播接收器应该接收文本,并根据文本启动或停止服务。如何做到这一点?
I have an activity. It contains a button whose text changes dynamically. I would like to pass this text to my broadcast receiver which receives the sms. Now my broadcast receiver should receive the text and based on the text it should start or stop a service. How to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的 BroadcastReceiver 是在单独的类文件中定义的,那么您可以简单地将值广播到该接收器。 实现服务魔法
收到值后,通过使用接收者的
context
更新:在您的活动中
:在您的接收者中:
在清单 xml 中:
if your BroadcastReceiver is defined in a separate class file, then you may simply broadcast the value to that receiver. Once the value is received, do the magic for service by using receiver's
context
Update:
in your activity:
in your receiver:
in manifest xml:
您可以让您的活动向接收器发送意图,并将文本作为额外的内容传递
,然后在接收器中,使用 startService 函数
You can have your activity send an intent to the receiver, and pass the text as an extra
And then in your receiver, start the service using the startService function