gpt4 book ai didi

android - 在 Android 中,是否可以在代码中向多个收件人发送短信?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:33:25 24 4
gpt4 key购买 nike

使用 smsManager 类可以同时向多个号码发送相同的短信吗?还是我需要做某种循环?

我想从我的应用程序中发送短信,而不是通过消息程序。

最佳答案

这里我附上源代码我所做的以编程方式发送消息。

短信:

public class SMS extends Activity {

Button btnSendSMS;
EditText txtPhoneNo;
EditText txtMessage;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
txtMessage = (EditText) findViewById(R.id.txtMessage);

btnSendSMS.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String message = txtMessage.getText().toString();
String phoneNo = txtPhoneNo.getText().toString();

StringTokenizer st=new StringTokenizer(phoneNo,",");
while (st.hasMoreElements())
{
String tempMobileNumber = (String)st.nextElement();
if(tempMobileNumber.length()>0 && message.trim().length()>0) {
sendSMS(tempMobileNumber, message);
}
else {
Toast.makeText(getBaseContext(),
"Please enter both phone number and message.",
Toast.LENGTH_SHORT).show();
}
}

// if (phoneNo.length()>0 && message.length()>0)
// sendSMS(phoneNo, message);
// else
// Toast.makeText(getBaseContext(),
// "Please enter both phone number and message.",
// Toast.LENGTH_SHORT).show();
}
});

}
private void sendSMS(String phoneNumber, 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);

//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
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));

//---when the SMS has been delivered---
registerReceiver(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;
}
}
}, new IntentFilter(DELIVERED));

SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}

}

短信接收器:

public class SmsReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
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 += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}

并且需要在 list 中设置权限READ_SMS,RECIEVE_SMS。这就是您可以向单个用户发送消息的方式..

这就是您可以向多个用户发送消息的方式。………………………………………………………………………………………………………………………………………………假设你需要向多个用户发送消息你可以用逗号分隔数字例如。 5558,5554。

这就是你可以使用逗号分隔两个数字的方法上面代码中使用的字符串分词器类

您可以在调用 SendSms 函数并每次传递适当的电话号码时应用此逻辑

对我来说很好用..你可以试试..它回答了你的问题吗?

谢谢,

拉克什

关于android - 在 Android 中,是否可以在代码中向多个收件人发送短信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3600326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com