如何用“;”向各种 phone 号码发送消息对于单独的 (java android)?
How send messages to various phone number with ";" for separate (java android)?
private void sendSMS(){
String phoneNo = number.getText().toString().trim();
String SMS = message.getText().toString().trim();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null,SMS,null,null);
Toast.makeText(this,"SMS est envoyé",Toast.LENGTH_SHORT).show();
} catch (Exception e){
e.printStackTrace();
Toast.makeText(this,"Erreur",Toast.LENGTH_SHORT).show();
}
}
您好,我有一段成功运行的代码!但我现在想用单独的“;”向各个号码发送消息。
例如,在模拟器中我想输入号码区 (1254;2058;153348),并且消息必须发送到我输入的所有联系人。
感谢您的回答!
phoneNo.split(";")
将为您提供包含多个联系人的 String
数组。使用 for
循环发送到多个 users/numbers
private void sendSMS(){
String phoneNo = number.getText().toString().trim();
String SMS = message.getText().toString().trim();
if(phoneNo.contains(";"){
String[] phoneNumbers = phoneNo.split(";");
for(String number : phoneNumbers) sendSmsTo(SMS, number);
}
else sendSmsTo(SMS, phoneNo);
}
private void sendSmsTo(String SMS, String phoneNo){
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null,SMS,null,null);
Toast.makeText(this,"SMS est envoyé",Toast.LENGTH_SHORT).show();
} catch (Exception e){
e.printStackTrace();
Toast.makeText(this,"Erreur",Toast.LENGTH_SHORT).show();
}
}
private void sendSMS(){
String phoneNo = number.getText().toString().trim();
String SMS = message.getText().toString().trim();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null,SMS,null,null);
Toast.makeText(this,"SMS est envoyé",Toast.LENGTH_SHORT).show();
} catch (Exception e){
e.printStackTrace();
Toast.makeText(this,"Erreur",Toast.LENGTH_SHORT).show();
}
}
您好,我有一段成功运行的代码!但我现在想用单独的“;”向各个号码发送消息。
例如,在模拟器中我想输入号码区 (1254;2058;153348),并且消息必须发送到我输入的所有联系人。
感谢您的回答!
phoneNo.split(";")
将为您提供包含多个联系人的 String
数组。使用 for
循环发送到多个 users/numbers
private void sendSMS(){
String phoneNo = number.getText().toString().trim();
String SMS = message.getText().toString().trim();
if(phoneNo.contains(";"){
String[] phoneNumbers = phoneNo.split(";");
for(String number : phoneNumbers) sendSmsTo(SMS, number);
}
else sendSmsTo(SMS, phoneNo);
}
private void sendSmsTo(String SMS, String phoneNo){
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null,SMS,null,null);
Toast.makeText(this,"SMS est envoyé",Toast.LENGTH_SHORT).show();
} catch (Exception e){
e.printStackTrace();
Toast.makeText(this,"Erreur",Toast.LENGTH_SHORT).show();
}
}