[英]How to send an array from broadcastReceiver class to MainActivity
I am working in an Android application that looks for pharmacies that have a specific medicine .all that throw SMS . 我正在使用一个Android应用程序,该应用程序查找具有特定药物的药房。 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. 因此该应用会收到一条短信,其中包含所有拥有该药的药房的名称,将它们保存在名为OurString的数组中,但此数组在广播接收器类中。我尝试发送一个字符串,它起作用了,但是当我尝试该数组时没有工作。所以我如何发送数组到其他类以在listView中查看药房的名称。
This is my BroadcastRecieve class: 这是我的BroadcastRecieve类别:
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: 这是我要发送String的地方,我使用药房数组来测试listview,并希望将其替换为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();
}
}
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);
I had faced same problem and setting flag as below worked for me 我遇到了同样的问题,并设置以下标志对我有用
Intent intent = new Intent(context,MainActivity.class); intent.putStringArrayListExtra("list", arraylistOfStrings); PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
In MainActivity 在MainActivity中
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
arrayList= bundle.getStringArrayList("list");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.