发送一条短信至多个电话号码
我有一个电话号码arraylist。 当我发送一条消息时,它只发送到列表中的最后一个数字。 这是我的代码
public void onActivityResult(int requestCode,int resultCode,Intent data){super.onActivityResult(requestCode,resultCode,data);
if(requestCode == CONTACT_PICK_REQUEST && resultCode == RESULT_OK){
ArrayList<Contact> selectedContacts = data.getParcelableArrayListExtra("SelectedContacts");
String display="";
for(int i=0;i<selectedContacts.size();i++){
display += (i+1)+" "+selectedContacts.get(i).toString()+"n";
theNumber=selectedContacts.get(i).phone;
}
contactsDisplay.setText("Selected Contacts : nn"+display);
}
}
protected void sendMsg(String theNumber, String myMsg) {
String SENT = "Message Sent";
String DELIVERED = "Message Delivered";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
registerReceiver(new BroadcastReceiver() {
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(MainActivity.this, "SMS SENT", Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(MainActivity.this, "Generic Failure", Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(MainActivity.this, "No Service", Toast.LENGTH_LONG).show();
break;
}
}
}, new IntentFilter(SENT));
registerReceiver(new BroadcastReceiver() {
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_LONG).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_LONG).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(theNumber, null, myMsg, sentPI, deliveredPI);
}
}
您的循环只是将列表中的数字替换为theNumber
变量。
theNumber=selectedContacts.get(i).phone;
循环结束时,它找到变量theNumber
用列表中的最后一个数字更新。