将参数从 Activity 传递到 BroadcastReceiver

发布于 2024-11-28 12:10:10 字数 486 浏览 1 评论 0原文

嘿,我一直在尝试将一个字符串数组从我的活动传递到广播接收器,但它总是在广播接收时给我 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

总攻大人 2024-12-05 12:10:10
public class GlobalVariable extends Application {

  private String[] var;

  public String[] getVar(){
    return var;
  }
  public void setVar(String[] var){
    this.var= var;
  }
}

并在我的活动中调用它

GlobalVariable appState = ((GlobalVariable) this.activity.getApplication());
appState.getVar() 

上面的代码行来检索类似的设置方式

public class GlobalVariable extends Application {

  private String[] var;

  public String[] getVar(){
    return var;
  }
  public void setVar(String[] var){
    this.var= var;
  }
}

And called it in my activity

GlobalVariable appState = ((GlobalVariable) this.activity.getApplication());
appState.getVar() 

above line of code to retrieve similar way to set

泅人 2024-12-05 12:10:10

首先,为什么要尝试从活动与广播接收器进行通信?这是极其不寻常的行为,以至于表明存在代码组织问题。

其次,为什么要使用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文