我有从 Android 发送 SMS 的应用程序。
代码 1
SmsManager sms = SmsManager.getDefault();
Intent intent = new Intent("smsSend");
intent.putExtra("id", id);
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
sms.sendTextMessage(phoneNo, null, messageText, null, null);
SendDbUserDao.getInstance(SmsSendIntentService.this).updateSendSMSStatus(intent.getExtras().get(Constants.id).toString(), Constants.sendStatus);
代码 2:
SmsManager sms = SmsManager.getDefault();
Intent intent = new Intent("smsSend");
intent.putExtra("id", id);
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if(messageText.isEmpty()){
SendDbUserDao.getInstance(SmsSendIntentService.this).updateSendSMSStatus(id, Constants.sendStatus);
}else {
System.out.println("SmsSendIntentService.sendSms()" + id);
sms.sendTextMessage(phoneNo, null, messageText, sentPI, null);
}
监听类
public class SmsSendListener extends BroadcastReceiver {
String tag="SmsSendListener";
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
if (intent.getExtras() != null) {
Log.i("Message Send Confirmation", intent.getExtras().get(Constants.id).toString());
SendDbUserDao.getInstance(context).updateSendSMSStatus(intent.getExtras().get(Constants.id).toString(), Constants.sendStatus);
}
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(context,"SMS Sending Error :Generic failure" ,Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(context,"SMS Sending Error :No service" ,Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(context,"SMS Sending Error :Null PDU" ,Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(context,"SMS Sending Error :Radio off" ,Toast.LENGTH_SHORT).show();
break;
}
}
问题1:逻辑上代码1和代码2相同??
问题 2:两者的输出非常不同 代码 1 只向每个号码发送一次短信,但代码 2 发送多次!为什么 ?