【发布时间】:2014-12-18 06:49:12
【问题描述】:
我想发送给多个收件人。 我还想使用内置的 SMS 机制,而不需要提示所需的应用程序(Whatsapp 等)
为了完成这个,我正在使用 Android 的 SmsManager。
for循环遍历手机号码的mobileList数组,并一一发送短信给每个手机号码。
BroadcastReceiver 检索已交付 SMS 的指示,以用于交付的ActionIntent 意图。
我正在祝酒词“已交付”和正在交付的消息的索引号。
我的问题是:
未显示实际索引 (idx)。我为所有 toasts 获得相同的索引号,即 mobileList 项目的数量。 为什么会这样?我希望每个手机本身都有索引。
mobileList 项目的数量有限制吗?例如,我可以有 200 人吗?
我在一个包含 4 个手机号码的列表上对此进行了测试,但随后我得到了 8-10 个祝酒词。我希望为一次移动交付干杯。 这里有什么问题?
如何在所有短信发送完毕后收到通知?我想这应该是像 AsyncTask 这样的后台操作。 有人可以告诉我怎么做吗?
SmsManager 的代码如下所示。
SmsManager smsManager = SmsManager.getDefault();
for(idx = 0; idx < mobileList.length; idx++) {
String toNumber = mobileList[idx];
String sms = message;
// SMS sent pending intent
Intent sentActionIntent = new Intent(SENT_ACTION);
sentActionIntent.putExtra(EXTRA_IDX, idx);
sentActionIntent.putExtra(EXTRA_TONUMBER, toNumber);
sentActionIntent.putExtra(EXTRA_SMS, sms);
PendingIntent sentPendingIntent = PendingIntent.getBroadcast(this, 0, sentActionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
/* Register for SMS send action */
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String result = "";
switch (getResultCode()) {
case Activity.RESULT_OK:
result = "Transmission successful";
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
result = "Transmission failed";
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
result = "Radio off";
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
result = "No PDU defined";
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
result = "No service";
break;
}
// Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
}
}, new IntentFilter(SENT_ACTION));
// SMS delivered pending intent
Intent deliveredActionIntent = new Intent(DELIVERED_ACTION);
deliveredActionIntent.putExtra(EXTRA_IDX, idx);
deliveredActionIntent.putExtra(EXTRA_TONUMBER, toNumber);
deliveredActionIntent.putExtra(EXTRA_SMS, sms);
PendingIntent deliveredPendingIntent = PendingIntent.getBroadcast(this, 0, deliveredActionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
/* Register for Delivery event */
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "Deliverd " + Integer.toString(idx), Toast.LENGTH_SHORT).show();
}
}, new IntentFilter(DELIVERED_ACTION));
//send
smsManager.sendTextMessage(toNumber, null, sms, sentPendingIntent, deliveredPendingIntent);
}
【问题讨论】:
标签: android sms confirmation smsmanager