将参数从 Activity 传递到 BroadcastReceiver
嘿,我一直在尝试将一个字符串数组从我的活动传递到广播接收器,但它总是在广播接收时给我 null 我已经尝试了 2-3 种方式。
// Code in Receiver
String stringText= intent.getExtras().getString("string_text");
//Code in Activity
Intent i = new Intent("android.intent.action.PHONE_STATE");
i.putExtra("string_text", "abc");
sendBroadcast(i);
但在接收端 stringText 总是为空。我以另一种方式尝试过,但没有运气
String text= (String)intent.getSerializableExtra("string_text");
,但到目前为止,没有人能帮助我解决这个问题吗?
Hey I have been trying to pass an array of string from my activity to broadcast receiver but it always give me null at broadcast receive i have tried it in 2-3 ways.
// Code in Receiver
String stringText= intent.getExtras().getString("string_text");
//Code in Activity
Intent i = new Intent("android.intent.action.PHONE_STATE");
i.putExtra("string_text", "abc");
sendBroadcast(i);
but at receiver end stringText always come null. I have tried it in another way but no luck
String text= (String)intent.getSerializableExtra("string_text");
But till now no luck can anyone help me with this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
并在我的活动中调用它
上面的代码行来检索类似的设置方式
And called it in my activity
above line of code to retrieve similar way to set
首先,为什么要尝试从活动与广播接收器进行通信?这是极其不寻常的行为,以至于表明存在代码组织问题。
其次,为什么要使用
android.intent.action.PHONE_STATE
?我相当有信心你不是在谷歌工作。因此,您不应该使用系统定义的操作字符串。事实上,这很可能就是你遇到困难的根源——我怀疑你能否发送该广播。First, why are you trying to communicate from an activity to a broadcast receiver? That is extremely unusual behavior, to the point that it suggests a code organization problem.
Second, why are you using
android.intent.action.PHONE_STATE
? I am fairly confident that you do not work for Google. Hence, you should not be using a system-defined action string. In fact, it is quite possible that this is the source of your difficulty -- I doubt that you can send that broadcast.