簡體   English   中英

如果消息已成功發送或發送,Android短信會收到響應錯誤/通知

[英]Android sms getting the response error/notification if the message has been delivered or sent successfully

How can i get the error when sending message and the response when the message has been successfully sent?

這是我的代碼:

try {    
   String message = "Hello World! Now we are going to demonstrate " + 
      "how to send a \n message with more than \n 160 characters from your Android application.";
   SmsManager smsManager = SmsManager.getDefault();
   ArrayList<String> parts = smsManager.divideMessage(message ); 
   smsManager.sendMultipartTextMessage(phoneNumber, null, parts, null, null);
} catch (Exception e) {
   //HOW CAN I GET THE SMS RESPONES HERE
   Toast.makeText(context, "SMS faild!",Toast.LENGTH_LONG).show();
   e.printStackTrace();
}

您不能catch已發送或傳遞的成功/失敗,因為它不是Exception ,也不是任何其他類型的Throwable

您將需要為已發送和已發送的操作創建PendingIntent ,並將其傳遞到用於發送SMS的方法中。 以下是可用於單部分或多部分消息的示例。

public static final String ACTION_SMS_SENT = "com.mycompany.myapp.SMS_SENT";
public static final String ACTION_SMS_DELIVERED = "com.mycompany.myapp.SMS_DELIVERED";

private void sendSMS(String number, String message) {
    final SmsManager sm = SmsManager.getDefault();        
    final ArrayList<String> parts = sm.divideMessage(message);
    final int ct = parts.size();

    final ArrayList<PendingIntent> sentPis = new ArrayList<PendingIntent>(ct);      
    final ArrayList<PendingIntent> delPis = new ArrayList<PendingIntent>(ct);       

    for (int i = 0; i < ct; i++) {
        final PendingIntent piSent =
            PendingIntent.getBroadcast(this,
                                       i,
                                       new Intent(ACTION_SMS_SENT),
                                       0);
        final PendingIntent piDel =
            PendingIntent.getBroadcast(this,
                                       i,
                                       new Intent(ACTION_SMS_DELIVERED),
                                       0);

        sentPis.add(piSent);
        delPis.add(piDel);
    }

    sm.sendMultipartTextMessage(number, null, parts, sentPis, delPis);
}

您將需要注冊一個BroadcastReceiver以獲得結果。 一個基本的接收器示例:

public class SmsResultReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (action.equals(ACTION_SMS_SENT)) {
            switch (getResultCode()) {
                case -1: //Activity.RESULT_OK
                    break;

                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    break;

                case SmsManager.RESULT_ERROR_NULL_PDU:
                    break;

                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    break;

                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    break;

                default:
            }
        }
        else if (action.equals(ACTION_SMS_DELIVERED)) {
            switch (getResultCode()) {
                case -1: //Activity.RESULT_OK
                    break;

                case 0: //Activity.RESULT_CANCELED
                    break;

                default:
            }
        }
    }
}

請注意,每個接收器將為每個消息部分運行一次。 我還要指出,並非所有運營商都提供交付報告,因此不能保證交付的 PendingIntent會觸發。

可以使用Context#registerReceiver()方法動態注冊此Receiver的實例,也可以使用<receiver>元素和相應的<intent-filter>在清單中注冊Receiver類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM