Android之發送短信的兩種方式


SMS涉及的主要類SmsManager

 

實現SMS主要用到SmsManager類,該類繼承自java.lang.Object類,下面我們介紹一下該類的主要成員。

 

公有方法:

 

  • ArrayList<String> divideMessage(String text) 
    當短信超過SMS消息的最大長度時,將短信分割為幾塊。 
    參數:text——初始的消息,不能為空 
    返回值:有序的ArrayList<String>,可以重新組合為初始的消息
  • static SmsManager getDefault() 
    獲取SmsManager的默認實例。 
    返回值:SmsManager的默認實例
  • void SendDataMessage(String destinationAddress, String scAddress, short destinationPort, byte[] data,PendingIntent sentIntent, PendingIntent deliveryIntent) 
    發送一個基於SMS的數據到指定的應用程序端口。 
    參數: 
    1)、destinationAddress——消息的目標地址 
    2)、scAddress——服務中心的地址or為空使用當前默認的SMSC 3)destinationPort——消息的目標端口號 
    4)、data——消息的主體,即消息要發送的數據 
    5)、sentIntent——如果不為空,當消息成功發送或失敗這個PendingIntent就廣播。結果代碼是Activity.RESULT_OK表示成功,或RESULT_ERROR_GENERIC_FAILURE、RESULT_ERROR_RADIO_OFF、RESULT_ERROR_NULL_PDU之一表示錯誤。對應RESULT_ERROR_GENERIC_FAILURE,sentIntent可能包括額外的“錯誤代碼”包含一個無線電廣播技術特定的值,通常只在修復故障時有用。 
    每一個基於SMS的應用程序控制檢測sentIntent。如果sentIntent是空,調用者將檢測所有未知的應用程序,這將導致在檢測的時候發送較小數量的SMS。 
    6)、deliveryIntent——如果不為空,當消息成功傳送到接收者這個PendingIntent就廣播。
    異常:如果destinationAddress或data是空時,拋出IllegalArgumentException異常。
  • void sendMultipartTextMessage(String destinationAddress, String scAddress, ArrayList<String> parts,ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent>  deliverIntents) 
    發送一個基於SMS的多部分文本,調用者應用已經通過調用divideMessage(String text)將消息分割成正確的大小。 
    參數: 
    1)、destinationAddress——消息的目標地址 
    2)、scAddress——服務中心的地址or為空使用當前默認的SMSC 
    3)、parts——有序的ArrayList<String>,可以重新組合為初始的消息 
    4)、sentIntents——跟SendDataMessage方法中一樣,只不過這里的是一組PendingIntent 
    5)、deliverIntents——跟SendDataMessage方法中一樣,只不過這里的是一組PendingIntent 
    異常:如果destinationAddress或data是空時,拋出IllegalArgumentException異常。
  • void sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent,PendingIntent deliveryIntent) 
    發送一個基於SMS的文本。參數的意義和異常前面的已存在的一樣,不再累述。

 

常量:

 

    • public static final int RESULT_ERROR_GENERIC_FAILURE 
      表示普通錯誤,值為1(0x00000001)
    • public static final int RESULT_ERROR_NO_SERVICE 
      表示服務當前不可用,值為4 (0x00000004)
    • public static final int RESULT_ERROR_NULL_PDU 
      表示沒有提供pdu,值為3 (0x00000003)
    • public static final int RESULT_ERROR_RADIO_OFF 
      表示無線廣播被明確地關閉,值為2 (0x00000002)
    • public static final int STATUS_ON_ICC_FREE 
      表示自由空間,值為0 (0x00000000)
    • public static final int STATUS_ON_ICC_READ 
      表示接收且已讀,值為1 (0x00000001)
    • public static final int STATUS_ON_ICC_SENT 
      表示存儲且已發送,值為5 (0x00000005)
    • public static final int STATUS_ON_ICC_UNREAD 
      表示接收但未讀,值為3 (0x00000003)
    • public static final int STATUS_ON_ICC_UNSENT 
      表示存儲但為發送,值為7 (0x00000007)

 

 

 

 

第一:調用系統短信接口直接發送短信;主要代碼如下: 

 

復制代碼
/**
     * 直接調用短信接口發短信
     * 
     * @param phoneNumber
     * @param message
     */
    public void sendSMS(String phoneNumber, String message) {
        // 獲取短信管理器
        android.telephony.SmsManager smsManager = android.telephony.SmsManager
                .getDefault();
        // 拆分短信內容(手機短信長度限制)
        List<String> divideContents = smsManager.divideMessage(message);
        for (String text : divideContents) {
            smsManager.sendTextMessage(phoneNumber, null, text, sentPI,
                    deliverPI);
        }
    }
復制代碼

 

第二:調起系統發短信功能;主要代碼如下: 

 

 

復制代碼
/**
     * 調起系統發短信功能
     * @param phoneNumber
     * @param message
     */
    public void doSendSMSTo(String phoneNumber,String message){
        if(PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber)){
            Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"+phoneNumber));          
            intent.putExtra("sms_body", message);          
            startActivity(intent);
        }
    }
復制代碼

 

 

 

下面來主要講解第一種方法,第一種方法可以監控發送狀態和對方接收狀態使用的比較多。

 

處理返回的狀態代碼如下: 

  

復制代碼
//處理返回的發送狀態 
        String SENT_SMS_ACTION = "SENT_SMS_ACTION";
        Intent sentIntent = new Intent(SENT_SMS_ACTION);
        sentPI= PendingIntent.getBroadcast(this, 0, sentIntent,
                0);
        // register the Broadcast Receivers
        this.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context _context, Intent _intent) {
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(MainActivity.this,
                "短信發送成功", Toast.LENGTH_SHORT)
                .show();
                break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                break;
                }
            }
        }, new IntentFilter(SENT_SMS_ACTION));

        
        //處理返回的接收狀態 
        String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
        // create the deilverIntent parameter
        Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
        deliverPI = PendingIntent.getBroadcast(this, 0,
               deliverIntent, 0);
        this.registerReceiver(new BroadcastReceiver() {
           @Override
           public void onReceive(Context _context, Intent _intent) {
               Toast.makeText(MainActivity.this,
          "收信人已經成功接收", Toast.LENGTH_SHORT)
          .show();
           }
        }, new IntentFilter(DELIVERED_SMS_ACTION));
復制代碼

 

別忘了權限的問題: 
<uses-permission android:name="android.permission.SEND_SMS" /> 

 


源碼下載地址:http://download.csdn.net/detail/zyw_java/8917057


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM