我正在开发一些Android应用程序,它通过Web服务获取一组SMS并发送它们,它被实现为服务,并且例如每2分钟调用一次,通过LogCat我可以看到消息被正确地检索并使用sendTextMessage发送,但是在电话上它并不总是有效,我确定他们从服务器拨打电话,但显然 sendTextMessage 和 sendMultipartTextMessage 不起作用,例如,当它收到 4 条短信发送时,它只发送其中一两条或有时没有,它随机播放,你知道我的问题在哪里吗?
服务 :
public class CopyOfSender extends Service {
static final String KEY_ITEM_SMSList = "SMS";
static final String KEY_Id = "Id";
static final String KEY_Message = "Message";
static final String KEY_Number = "Number";
String SmsURL = "GetSMS/";
String UpdateMessageStatuseURL = "UpdateSMSStatus";
ConnectionDetector cd;
String userNumner = "";
String password = "";
String smsPerSend = "20";
Context thisContext = null;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
thisContext = getApplicationContext();
PowerManager pm = (PowerManager) getApplicationContext()
.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, "");
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
if (pref.getString("smsPerSend", null) != null) {
smsPerSend = pref.getString("smsPerSend", "");
}
cd = new ConnectionDetector(getApplicationContext());
String BaseUrl = getApplicationContext().getResources().getString(
R.string.baseURl);
SmsURL = BaseUrl + SmsURL;
UpdateMessageStatuseURL = BaseUrl + UpdateMessageStatuseURL;
wl.acquire();
if (pref.getString("phonenumber", null) == null
|| pref.getString("password", null) == null) {
} else {
userNumner = pref.getString("phonenumber", "");
password = pref.getString("password", "");
if (!cd.isConnectingToInternet()) {
Toast.makeText(getApplicationContext(),
getApplicationContext().getString(R.string.noInternet),
Toast.LENGTH_LONG).show();
} else {
new SendSMS().execute();
}
}
wl.release();
}
private class Message {
public String Id;
public String Message;
public String Number;
}
private class SendSMS extends AsyncTask<Object, Object, Object> {
List<Message> messages = new ArrayList<Message>();
@Override
protected Object doInBackground(Object... arg0) {
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(SmsURL + "?number=" + userNumner
+ "&password=" + password + "&count=" + smsPerSend);
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_ITEM_SMSList);
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
String tempId = parser.getValue(e, KEY_Id);
String tempMessage = parser.getValue(e, KEY_Message);
String tempNumber = parser.getValue(e, KEY_Number);
Message tempSMS = new Message();
tempSMS.Id = tempId;
tempSMS.Message = tempMessage;
tempSMS.Number = tempNumber;
messages.add(tempSMS);
}
for (Message item : messages) {
if (item.Number.trim().length() != 0) {
if (item.Message.trim().length() != 0) {
sendSMS(item.Number.trim(), item.Message.trim(),
Integer.parseInt(item.Id));
}
}
}
return null;
}
protected void onPostExecute(Object result) {
return;
}
}
private class UpdateSMSStat extends AsyncTask<Object, Object, Object> {
@Override
protected Object doInBackground(Object... arg0) {
int smsId = (Integer) arg0[0];
int stat = (Integer) arg0[1];
XMLParser parser = new XMLParser();
List<NameValuePair> IdsToSend = new ArrayList<NameValuePair>();
IdsToSend.add(new BasicNameValuePair("MessageIds", Integer
.toString(smsId)));
IdsToSend.add(new BasicNameValuePair("Status", Integer
.toString(stat)));
IdsToSend.add(new BasicNameValuePair("Number", userNumner));
IdsToSend.add(new BasicNameValuePair("Password", password));
parser.sendPostRequest(UpdateMessageStatuseURL, IdsToSend);
return null;
}
}
private void sendSMS(final String phoneNumber, String message,
final int messageId) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(thisContext, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(thisContext, 0,
new Intent(DELIVERED), 0);
thisContext.registerReceiver(new BroadcastReceiver() {
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
break;
}
}
}, new IntentFilter(SENT));
thisContext.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
new UpdateSMSStat().execute(messageId, 1, phoneNumber);
break;
case Activity.RESULT_CANCELED:
new UpdateSMSStat().execute(messageId, 3, phoneNumber);
break;
}
}
}, new IntentFilter(DELIVERED));
Pattern p = Pattern
.compile("[^a-zA-Z0-9!@#$%^&*()_+-=\\|`~{};':,.<>/?/[/]\" ]");
boolean hasSpecialChar = p.matcher(message).find();
SmsManager sms = SmsManager.getDefault();
if (hasSpecialChar) {
if (message.length() > 70) {
ArrayList<String> parts = sms.divideMessage(message);
ArrayList<PendingIntent> sentPis = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliverPis = new ArrayList<PendingIntent>();
sentPis.add(sentPI);
deliverPis.add(deliveredPI);
sms.sendMultipartTextMessage(phoneNumber, null, parts, sentPis,
deliverPis);
} else {
sms.sendTextMessage(phoneNumber, null, message.toString(),
sentPI, deliveredPI);
}
} else {
if (message.length() > 160) {
ArrayList<String> parts = sms.divideMessage(message);
ArrayList<PendingIntent> sentPis = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliverPis = new ArrayList<PendingIntent>();
sentPis.add(sentPI);
deliverPis.add(deliveredPI);
sms.sendMultipartTextMessage(phoneNumber, null, parts, sentPis,
deliverPis);
} else {
sms.sendTextMessage(phoneNumber, null, message.toString(),
sentPI, deliveredPI);
}
}
}
}