我正在开发一个短信发送应用程序。代码运行正常。我可以从我的应用程序发送短信 为此,我使用以下代码:
private void sendSMS(String phoneNumber, String message, final int k){
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
Intent sentIntent = new Intent(SENT);
Intent deliveredUntent = new Intent(DELIVERED);
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,sentIntent, 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,deliveredUntent, 0);
//---when the SMS has been sent---
sentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()){
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent "+k,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;
}
}
};
//---when the SMS has been delivered---
deliverReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
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;
}
}
};
try{
registerReceiver(sentReceiver, new IntentFilter(SENT));
registerReceiver(deliverReceiver, new IntentFilter(DELIVERED));
} catch (Exception e) {
e.printStackTrace();
}
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
但现在我想将短信发送给多个号码。我怎样才能做到这一点。我在Google上搜索但没有得到满意的答案。
答案 0 :(得分:4)
简单地循环包含多个数字的列表并在循环中调用sms.sendtextmessage
答案 1 :(得分:0)
public void sendSMS(String number2 [],String message){
for (int i = 0; i < number2.length; i++) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number2[i], null, message, null,
null);
Toast.makeText(getApplicationContext(), "SMS Sent to" + " " + number2[1],
Toast.LENGTH_LONG).show();
strNuum = strNuum + 1;
btnNumber.setText("Sending " + strNuum + " " + "TO" + " "
+ StringArrayNumber.length);
edtName.setText("TO: " + number2[1]);
new Handler().postDelayed(new Runnable() {
public void run() {
}
}, DISPLAY_TIME);
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!", Toast.LENGTH_LONG)
.show();
e.printStackTrace();
}
}
}