我目前正在开发一个短信应用程序,并允许发送短信给多人的用户。
I am currently trying to develop an SMS application and allow a user to send SMS to multiple people.
由于短信是长我必须使用sendMultipartTextMessage,低于
As the SMS is long i have to use the sendMultipartTextMessage which is below
private void sendSMS(final String phoneNumber, String message) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(message);
int messageCount = parts.size();
Log.i("Message Count", "Message Count: " + messageCount);
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
for (int j = 0; j < messageCount; j++) {
sentIntents.add(sentPI);
deliveryIntents.add(deliveredPI);
}
// ---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));
sms.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents, deliveryIntents);
}
我会遇到一个通用的错误,当我试图将其在循环中这样发送给多个人的消息将不会被发送。我怀疑这是因为消息可能是3-4件和android系统甚至无法与3000毫秒的延迟发送消息的时间。
I will encounter a generic error when i attempt to send it to multiple people in a loop like this and the message won't be sent. I'm suspecting it is because the message is probably 3-4 parts and the android system cannot send the message in time even with the 3000 milliseconds delay.
for (int t = 0; t < array.length; t++) {
System.out.println("temp: " + array[t].toString());
try {
sendSMS(array[t].toString(), message);
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
编辑:上面的code是在其服务中执行一个AsyncTask的
The above code are in a AsyncTask which is executed inside a Service.
推荐答案
IHMO没有使用正确的方法。我使用的是不同的一个,这是工作。我试着解释:
IHMO you are not using the right approach. I'm using a different one and it is working. I try to explain:
您应该保持一个计数器所有待处理的意图,因为如果您发送的短信主要有两部分,您将收到两个RESULT_OK(或RESULT_ERROR_ *),只有当您收到结果所有部分你应该继续发送下一个短信。我不喜欢阻塞线程的想法...也许这就是你得到奇怪的错误的原因。
You should keep a counter for all pending intents because if the sms you are sending has two parts you will receive two RESULT_OK (or RESULT_ERROR_*) and only when you receive the results for all parts you should proceed sending the next sms. I don't like the idea of blocking the thread... maybe this is the reason you are getting strange errors.
我重构你的code用我的方法:
I refactored your code with my approach:
private int mMessageSentParts;
private int mMessageSentTotalParts;
private int mMessageSentCount;
private void startSendMessages(){
registerBroadCastReceivers();
mMessageSentCount = 0;
sendSMS(array[mMessageSentCount].toString(), message);
}
private void sendNextMessage(){
if(thereAreSmsToSend()){
sendSMS(array[mMessageSentCount].toString(), message);
}else{
Toast.makeText(getBaseContext(), "All SMS have been sent",
Toast.LENGTH_SHORT).show();
}
}
private boolean thereAreSmsToSend(){
return mMessageSentCount < array.length;
}
private void sendSMS(final String phoneNumber, String message) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(message);
mMessageSentTotalParts = parts.size();
Log.i("Message Count", "Message Count: " + mMessageSentTotalParts);
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
for (int j = 0; j < mMessageSentTotalParts; j++) {
sentIntents.add(sentPI);
deliveryIntents.add(deliveredPI);
}
mMessageSentParts = 0;
sms.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents, deliveryIntents);
}
private void registerBroadCastReceivers(){
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
mMessageSentParts++;
if ( mMessageSentParts == mMessageSentTotalParts ) {
mMessageSentCount++;
sendNextMessage();
}
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));
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));
}
相关推荐
最新文章