问题
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