Passing Parameters from An Activity to BroadcastReceiver

不打扰是莪最后的温柔 提交于 2019-12-11 07:43:49

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/6975954/passing-parameters-from-an-activity-to-broadcastreceiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!