android API 中提供了SmsManager類處理簡訊。其中的sendTextMessage(num, null, content, pend, null)函數就是發送,具體介紹如下:
SMS涉及的主要類SmsManager
實現SMS主要用到SmsManager類,該類繼承自java.lang.Object類,下面我們介紹一下該類的主要成員。
公有方法:
1、ArrayList<String> divideMessage(String text)
當簡訊超過SMS訊息的最大長度時,將簡訊分割為幾塊。
參數:text——初始的訊息,不可為空
傳回值:有序的ArrayList<String>,可以重新組合為初始的訊息
2、static SmsManager getDefault()
擷取SmsManager的預設執行個體。
傳回值:SmsManager的預設執行個體
3、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異常。
4、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異常。
5、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));
以下是對以上代碼的封裝
1、簡訊的發送
import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.telephony.SmsManager;import java.util.ArrayList;import java.util.List;/** * Created by Javen on 2016-03-15. */public class SMSMethod { private static SMSMethod mSMSmsMethod; /* 自訂ACTION常數,作為廣播的Intent Filter識別常數 */ public static String SMS_SEND_ACTIOIN = "SMS_SEND_ACTIOIN"; public static String SMS_DELIVERED_ACTION = "SMS_DELIVERED_ACTION"; /* 建立兩個mServiceReceiver對象,作為類成員變數 */ private SMSReceiver mSendSMSReceiver, mDeliveredSMSReceiver; private Context mContext; private SMSMethod(Context context){ mContext=context; registerReceiver(); } public static SMSMethod getInstance(Context context){ if (mSMSmsMethod==null){ synchronized (SMSMethod.class){ if (mSMSmsMethod==null){ mSMSmsMethod=new SMSMethod(context); } } } return mSMSmsMethod; } /** * 註冊 */ public void registerReceiver(){ /* 自訂IntentFilter為SENT_SMS_ACTIOIN Receiver */ IntentFilter mFilter01; mFilter01 = new IntentFilter(SMS_SEND_ACTIOIN); mSendSMSReceiver = new SMSReceiver(); mContext.registerReceiver(mSendSMSReceiver, mFilter01); /* 自訂IntentFilter為DELIVERED_SMS_ACTION Receiver */ mFilter01 = new IntentFilter(SMS_DELIVERED_ACTION); mDeliveredSMSReceiver = new SMSReceiver(); mContext.registerReceiver(mDeliveredSMSReceiver, mFilter01); } public void unregisterReceiver(){ /* 取消註冊自訂Receiver */ if (mSendSMSReceiver!=null){ mContext.unregisterReceiver(mSendSMSReceiver); } if (mDeliveredSMSReceiver!=null) { mContext.unregisterReceiver(mDeliveredSMSReceiver); } } public void SendMessage(String strDestAddress,String strMessage){ /* 建立SmsManager對象 */ SmsManager smsManager = SmsManager.getDefault(); try { /* 建立自訂Action常數的Intent(給PendingIntent參數之用) */ Intent itSend = new Intent(SMS_SEND_ACTIOIN); Intent itDeliver = new Intent(SMS_DELIVERED_ACTION); /* sentIntent參數為傳送後接受的廣播資訊PendingIntent */ PendingIntent mSendPI = PendingIntent.getBroadcast(mContext, 0, itSend, 0); /* deliveryIntent參數為送達後接受的廣播資訊PendingIntent */ PendingIntent mDeliverPI = PendingIntent.getBroadcast(mContext, 0, itDeliver, 0); List<String> divideContents = smsManager.divideMessage(strMessage); for (String text:divideContents) { /* 發送SMS簡訊,注意倒數的兩個PendingIntent參數 */ smsManager.sendTextMessage(strDestAddress, null, text, mSendPI, mDeliverPI); } }catch(Exception e) { e.printStackTrace(); } } public void SendMessage2(String strDestAddress,String strMessage){ ArrayList<PendingIntent> sentPendingIntents = new ArrayList<PendingIntent>(); ArrayList<PendingIntent> deliveredPendingIntents = new ArrayList<PendingIntent>(); /* 建立SmsManager對象 */ SmsManager smsManager = SmsManager.getDefault(); try { /* 建立自訂Action常數的Intent(給PendingIntent參數之用) */ Intent itSend = new Intent(SMS_SEND_ACTIOIN); Intent itDeliver = new Intent(SMS_DELIVERED_ACTION); /* sentIntent參數為傳送後接受的廣播資訊PendingIntent */ PendingIntent mSendPI = PendingIntent.getBroadcast(mContext, 0, itSend, 0); /* deliveryIntent參數為送達後接受的廣播資訊PendingIntent */ PendingIntent mDeliverPI = PendingIntent.getBroadcast(mContext, 0, itDeliver, 0); ArrayList<String> mSMSMessage = smsManager.divideMessage(strMessage); for (int i = 0; i < mSMSMessage.size(); i++) { sentPendingIntents.add(i, mSendPI); deliveredPendingIntents.add(i, mDeliverPI); } /* 發送SMS簡訊,注意倒數的兩個PendingIntent參數 */ smsManager.sendMultipartTextMessage(strDestAddress,null,mSMSMessage ,sentPendingIntents,deliveredPendingIntents); }catch(Exception e) { e.printStackTrace(); } }}
2、簡訊發送狀態的監聽
package com.javen.sms.receiver;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.telephony.SmsManager;import android.widget.Toast;/** * Created by Javen on 2016-03-15. */public class SMSReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(SMSMethod.SMS_SEND_ACTIOIN)){ try{ /* android.content.BroadcastReceiver.getResultCode()方法 */ //Retrieve the current result code, as set by the previous receiver. switch (getResultCode()){ case Activity.RESULT_OK: System.out.println("簡訊發送成功"); Toast.makeText(context, "簡訊發送成功", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: System.out.println("簡訊發送失敗"); Toast.makeText(context, "簡訊發送失敗", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_RADIO_OFF: break; case SmsManager.RESULT_ERROR_NULL_PDU: break; } }catch (Exception e){ e.printStackTrace(); } } else if(intent.getAction().equals(SMSMethod.SMS_DELIVERED_ACTION)){ /* android.content.BroadcastReceiver.getResultCode()方法 */ switch(getResultCode()){ case Activity.RESULT_OK: System.out.println("簡訊已送達"); Toast.makeText(context, "簡訊已送達", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: System.out.println("簡訊未送達"); /* 簡訊未送達 */ Toast.makeText(context, "簡訊未送達", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_RADIO_OFF: break; case SmsManager.RESULT_ERROR_NULL_PDU: break; } } }}
測試代碼:
public void sendTextMessage(View view){ SMSMethod.getInstance(this).SendMessage("xxxx","測試簡訊。。。"); } public void sendMultipartTextMessage(View view){ SMSMethod.getInstance(this).SendMessage2("xxxx", "測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。測試簡訊wwww。。。"); } @Override protected void onPause() { SMSMethod.getInstance(this).unregisterReceiver(); super.onPause(); }
別忘了許可權的問題:
<uses-permission android:name="android.permission.SEND_SMS" />
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。