我是Android的初学者,我正在寻找具有特定药物的药房的应用程序。所有抛出短信。所以应用程序收到一条短信,其中包含所有药房的名称,将它们保存在一个名为OurString的数组中,但这个数组是广播接收器类。我有trid发送字符串,但是当我尝试了数组时没有工作。所以如何将数组发送到其他类,以查看列表视图中的药店名称。
这是我的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);
}
}
这是我想发送String的地方,我使用pharmacies数组来测试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();
}
}
答案 0 :(得分:0)
您可以发送和接收这样的数组:
发送:
Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);
接收:
Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);
答案 1 :(得分:0)
我遇到了同样的问题,并为我设置了以下标志
{Main}中的Intent intent = new Intent(context,MainActivity.class);
intent.putStringArrayListExtra("list", arraylistOfStrings);
PendingIntent pIntent = PendingIntent.getActivity(this, 0,
intent,PendingIntent.FLAG_CANCEL_CURRENT);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
arrayList= bundle.getStringArrayList("list");
}