我使用sendTextMessage方法发送短信。如下:
PendingIntent pi = PendingIntent.getBroadcast(context,0,new Intent("myIntentAction"),0); 
sManager.sendTextMessage(msg.getDestAddr(), null, msg.getBody(), pi, null);
我的问题是,怎么判断该短信是否发送成功。是不是用一个broadcast来监听呢?
是不是发送成功了才广播?
那怎么判断短信发送失败了呢?就是说,我调用了sendTextMessage方法后,怎么判断短信是发送成功了还是发送失败了?
谢谢。

解决方案 »

  1.   

    呵呵,我来说一下吧,可以结贴了,给分吧
    首先给你说文档里面明明就给你说了,哈哈
    sentIntent  if not NULL this PendingIntent is broadcast when the message is sucessfully sent, or failed. The result code will be Activity.RESULT_OK for success, or one of these errors:
    RESULT_ERROR_GENERIC_FAILURE
    RESULT_ERROR_RADIO_OFF
    RESULT_ERROR_NULL_PDU
    For RESULT_ERROR_GENERIC_FAILURE the sentIntent may include the extra "errorCode" containing a radio technology specific value, generally only useful for troubleshooting.
    The per-application based SMS control checks sentIntent. If sentIntent is NULL the caller will be checked against all unknown applications, which cause smaller number of SMS to be sent in checking period. 如果没看懂英文的话,我给你解释一下,实现步骤:
    一:sentIntent = PendingIntent.getBroadcast(this, 0,
                                        intent, PendingIntent.FLAG_UPDATE_CURRENT);
    这里面的intent,要给一个广播,当然一个activity也可以,广播如下:
    Intent intent = new Intent(this, ReceiveSMSBroadcast.class);
    知道广播吧?
    public class ReceiveSMSBroadcast extends BroadcastReceiver
    别忘记配置
    <receiver android:name=".activity.receiver.ReceiveSMSBroadcast"/>
    二:发送后自动调用广播,利用广播接收数据:
     @Override
        public void onReceive(Context context, Intent intent) {
    用这个方法接收数据,哈哈,我真废话
    int resultCode = getResultCode()
    这是你接收的状态,状态就如上面的英文文档那几种状态
    Activity.RESULT_OK
    SmsManager.RESULT_ERROR_GENERIC_FAILURE:
    SmsManager.RESULT_ERROR_RADIO_OFF
    SmsManager.RESULT_ERROR_NULL_PDU
    SmsManager.RESULT_ERROR_NO_SERVICE当然你也可以利用intent Extra 传递携带一些其它你想要的数据
      

  2.   


    我完善一下,给出代码如下:
    package lab.sodino.smslistener;import android.app.Activity;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.telephony.gsm.SmsManager;
    import android.view.View;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.Button;
    import android.widget.LinearLayout;
    import android.widget.TextView;public class SmsAct extends Activity {
    private TextView textView;
    private static String ACTION_SMS_SEND = "lab.sodino.sms.send";
    private static String ACTION_SMS_DELIVERY = "lab.sodino.sms.delivery";
    private SMSReceiver sendReceiver;
    private SMSReceiver deliveryReceiver; public class SMSReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
    String actionName = intent.getAction();
    int resultCode = getResultCode();
    if (actionName.equals(ACTION_SMS_SEND)) {
    switch (resultCode) {
    case Activity.RESULT_OK:
    textView.append("\n[Send]SMS Send:Successed!");
    break;
    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
    textView
    .append("\n[Send]SMS Send:RESULT_ERROR_GENERIC_FAILURE!");
    break;
    case SmsManager.RESULT_ERROR_NO_SERVICE:
    textView
    .append("\n[Send]SMS Send:RESULT_ERROR_NO_SERVICE!");
    break;
    case SmsManager.RESULT_ERROR_NULL_PDU:
    textView.append("\n[Send]SMS Send:RESULT_ERROR_NULL_PDU!");
    break;
    case SmsManager.RESULT_ERROR_RADIO_OFF:
    break;
    }
    } else if (actionName.equals(ACTION_SMS_DELIVERY)) {
    switch (resultCode) {
    case Activity.RESULT_OK:
    textView.append("\n[Delivery]SMS Delivery:Success!");
    break;
    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
    textView
    .append("\n[Delivery]SMS Delivery:RESULT_ERROR_GENERIC_FAILURE!");
    break;
    case SmsManager.RESULT_ERROR_NO_SERVICE:
    textView
    .append("\n[Delivery]SMS Delivery:RESULT_ERROR_NO_SERVICE!");
    break;
    case SmsManager.RESULT_ERROR_NULL_PDU:
    textView
    .append("\n[Delivery]SMS Delivery:RESULT_ERROR_NULL_PDU!");
    break;
    case SmsManager.RESULT_ERROR_RADIO_OFF:
    textView
    .append("\n[Delivery]SMS Delivery:RESULT_ERROR_RADIO_OFF!");
    break;
    }
    }
    }
    } /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Button btn = new Button(this);
    btn.setOnClickListener(new Button.OnClickListener() {
    public void onClick(View v) {
    sendSMS();
    }
    });
    textView = new TextView(this);
    textView.setBackgroundColor(0xffffffff);
    textView.setTextColor(0xff0000ff);
    textView.setText("SMS processing...");
    btn.setText("发送短信");
    LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(
    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); LinearLayout linear = new LinearLayout(this);
    linear.setOrientation(LinearLayout.VERTICAL);
    linear.setLayoutParams(textParams);
    linear.addView(btn);
    linear.addView(textView);
    setContentView(linear);
    sendReceiver = new SMSReceiver();
    IntentFilter sendFilter = new IntentFilter(ACTION_SMS_SEND);
    registerReceiver(sendReceiver, sendFilter);
    deliveryReceiver = new SMSReceiver();
    IntentFilter deliveryFilter = new IntentFilter(ACTION_SMS_DELIVERY);
    registerReceiver(deliveryReceiver, deliveryFilter);
    } private void sendSMS() {
    String smsAddress = "10086";
    String smsBody = "bylcx";
    SmsManager smsMag = SmsManager.getDefault();
    Intent sendIntent = new Intent(ACTION_SMS_SEND);
    PendingIntent sendPI = PendingIntent.getBroadcast(this, 0, sendIntent,
    0);
    Intent deliveryIntent = new Intent(ACTION_SMS_DELIVERY);
    PendingIntent deliveryPI = PendingIntent.getBroadcast(this, 0,
    deliveryIntent, 0);
    smsMag.sendTextMessage(smsAddress, null, smsBody, sendPI, deliveryPI);
    } protected void onPaused() {
    unregisterReceiver(sendReceiver);
    unregisterReceiver(deliveryReceiver);
    }
    }
      

  3.   

    delivery的消息我怎么没有捕捉到过啊~
      

  4.   


    中国移动有delivery
    中国联通没有
      

  5.   

    想问一下哦,发短信就下面这句中的   sendPI 和 deliveryPI 是干什么的,如果这两个参数都为null 时,短信也可以发出去,这两个参数是干什么用的啊? smsMag.sendTextMessage(smsAddress, null, smsBody, sendPI, deliveryPI);