问题
I am working in an Android application that looks for pharmacies that have a specific medicine .all that throw SMS . so the app receive a SMS that contains the name of all pharmacies that have the medicine,saved them in an array named OurString but this array is in broadcast receiver class .i have trid to send a string and it worked but when i tried the array didnt worked.so how do I send array to other class to view the pharmacies's names in a listView.
This is my BroadcastRecieve class:
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null ;
String[] OurString = null;
String str = " ";
if(bundle != null)
{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0;i<msgs.length;i++)
{
msgs[i]= SmsMessage.createFromPdu((byte[])pdus[i]);
str += msgs[i].getMessageBody().toString();
OurString = msgs[i].getMessageBody().toString().split(" ");
str += " : ";
}
for(int i=0;i<OurString.length;i++)
{
str += "(";
str += OurString[i];
str += ")";
}
Toast.makeText(context,str,Toast.LENGTH_SHORT).show();
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("sms", str);
context.sendBroadcast(broadcastIntent);
}
}
This is where I want to send String, I used pharmacies array to test the listview and I want to replace it with the OurString:
public class MedName extends ListActivity {
String[] pharmacies = {"pharmacy 1","pharmacy 2"};
Button btnSendSMS,btnShowMap;
EditText editText;
IntentFilter intentFilter,intentFilter1;
private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
TextView SMSes = (TextView)findViewById(R.id.tv);
SMSes.setText(intent.getExtras().getString("sms"));
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.medicinename);
btnShowMap = (Button) findViewById(R.id.btnShowMap);
btnShowMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent showmap = new Intent("com.example.rana.me_lo.MapsActivity");
startActivity(showmap);
}
});
btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
editText = (EditText) findViewById(R.id.etMeName);
btnSendSMS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendSMS("5666",editText.getText().toString());
}
});
TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
tabHost.setup();
TabHost.TabSpec spec1 = tabHost.newTabSpec("Search");
spec1.setContent(R.id.tab1);
spec1.setIndicator(" Search",getResources().getDrawable(R.drawable.picture));
TabHost.TabSpec spec2 = tabHost.newTabSpec("Result");
spec2.setContent(R.id.tab2);
spec2.setIndicator("Result");
tabHost.addTab(spec1);
tabHost.addTab(spec2);
intentFilter =new IntentFilter();
intentFilter.addAction("SMS_RECEIVED_ACTION");
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, pharmacies));
}
private void sendSMS(String number, String message) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this,0,new Intent(SENT),0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this,0,new Intent(DELIVERED),0);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(),"SMS sent",Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
break;
}
}
},new IntentFilter(SENT));
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
break;
}
}
},new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(number,null,message,sentPI,deliveredPI);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(this,"You have selected "+pharmacies[position],Toast.LENGTH_SHORT).show();
}
@Override
protected void onResume() {
registerReceiver(intentReceiver,intentFilter);
super.onResume();
}
@Override
protected void onPause() {
unregisterReceiver(intentReceiver);
super.onPause();
}
}
回答1:
You can send and receive array like this:
Send:
Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);
Receive:
Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);
回答2:
I had faced same problem and setting flag as below worked for meIntent intent = new Intent(context,MainActivity.class);
intent.putStringArrayListExtra("list", arraylistOfStrings);
PendingIntent pIntent = PendingIntent.getActivity(this, 0,
intent,PendingIntent.FLAG_CANCEL_CURRENT);
In MainActivity
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
arrayList= bundle.getStringArrayList("list");
}
来源:https://stackoverflow.com/questions/30953669/how-to-send-an-array-from-broadcastreceiver-class-to-mainactivity