我指的是从安卓发送短信的this文章。
我试着发送10条短信到10个不同的号码与实现循环。例如,在sendSMS(phoneNo, message);
上循环,但在发送6条短信后,sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
提供NullPointerExeption。
仅供参考,我在deliveredPI中传递的是null,因为我不关心短信的发送。但是知道sms是否真的被发送是非常重要的,这可以通过注册广播接收器来检索。
我注册了一个广播接收器,但似乎整个过程没有同步。我们应该只发送第二个短信后,我们收到了以前的短信状态。
我假设放慢发送短信的速度将帮助我删除NullPointerExeption。等待状态将是保持发送另一条sms之间的时间间隔的最好主意。
简而言之,我想做的是发送一条短信,等待状态->更新状态在数据库中->发送另一条短信。
发布于 2011-12-12 16:17:29
我个人还没有测试我发布的代码,但它被接受为this other question的答案,所以我假设它会工作。
SmsManager smsMan = new SmsManager.getDefault();
ArrayList<String> contactList = new ArrayList();
//add contacts to contactList with contactList.add(string)
for (int i = 0; i <= contactList().size(); i++) {
String SENT = contactList.get(i).toString();// you could replace this with i,
//or something like "sms_sent_myappname" + i.toString());
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT, 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));
smsManager.sendTextMessage(contactList.get(i).toString(), null, message, sentPI, null);
}
如上所述,您将向Android发送一个请求,让其逐条发送每条消息。如果你真的想在Activity.RESULT_OK
之后发送下一条短信,那么我建议你仍然使用ArrayList
方法,但是不要使用for循环,你可以这样做:
public void onCreate(Bundle savedInstanceState) {
smsMan = new SmsManager.getDefault(); //assuming you declared SmsManager smsMan in class body.
contactList = new ArrayList(); //assuming you declared ArrayList<String> contactList in class body.
//add contacts to contactList with contactList.add(string);
}
public void sendSms(int position){
//add contacts to contactList with contactList.add(string)
String SENT = contactList.get(position).toString();// you could replace this with i,
//or something like "sms_sent_myappname" + i.toString());
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT, 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK
context.unregisterReceiver(this);
i++;
if (contactList.size()<i){
sendSms(i);
} else {
//You are done sending - Do what you want.
}
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));
smsManager.sendTextMessage(contactList.get(position).toString(), null, message, sentPI, null);
}
再来一次。我还没有测试它,但它应该可以工作。如果你还有什么问题,或者我有什么不清楚的,请告诉我。
发布于 2012-09-04 15:08:11
我把我的评论作为答案,因为我实际上不能评论。Jakar,你提供的解决方案将不起作用。您不能在任何地方注销广播接收器。您的代码将引发错误。
发布于 2012-10-24 04:16:03
我想你已经找到答案了。但由于问题仍然存在……它会使用sendMultipartTextMessage()吗?使用sendTextMessage()时有160个符号的限制,这是标准的单条短信的最大长度。
https://stackoverflow.com/questions/8471175
复制相似问题