Android開發之SmsManager(簡訊管理器)詳解,androidsmsmanager

來源:互聯網
上載者:User

Android開發之SmsManager(簡訊管理器)詳解,androidsmsmanager

        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的文本。參數的意義和異常前面的已存在的一樣,不再累述。

 關於SmsManager的應用執行個體大家可以參照:Android開發之傳送簡訊


Android開發中怎調用發簡訊功可以 詳細??

首先,應該在程式資訊清單檔AndroidManifest.xml 中加入發簡訊的許可權 圖1 圖1 為發簡訊的簡要介面 包括兩個TextView 組件,兩個EditText 組件,一個Button 組件,在主程式為發送按鈕增加單擊事件 private EditText txt_num; private EditText txt_content; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); txt_num= (EditText) this.findViewById(R.id.txt_num); txt_content=(EditText) this.findViewById(R.id.txt_content); Button btn_send = (Button) this.findViewById(R.id.btn_send); btn_send.setOnClickListener(new OnClickListener() { public void onClick(View v) { String str_num = txt_num.getText().toString();//得到電話號碼 String str_content = txt_content.getText().toString();//得到簡訊內容 SmsManager manager_sms = SmsManager.getDefault();//得到簡訊管理器 //由於簡訊可能較長,故將簡訊拆分 ArrayList texts = smsManager.divideMessage(str_content); for(String text : texts){ smsManager.sendTextMessage(str_num, null, text, null, null);//分別發送每一條簡訊 } Toast.makeText(SMSActivity.this, "發送成功!", Toast.LENGTH_LONG).show();// 提示成功 } }); } 至此,傳送簡訊功能介紹完畢
 
android 多條簡訊息發出後的狀態怎判斷?? <activity android:name="SmsActivity" android:label=

  SmsManager smsManager = SmsManager.getDefault();//擷取簡訊管理器,是Android系統預設的簡訊管理器,可以通過它傳送簡訊。
  List<String> contents = smsManager.divideMessage(content);//多條簡訊。這裡是將內容較長的簡訊拆分成多條簡訊發送。
  for(String text : contents){
  smsManager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);//傳送簡訊
  }

  傳送簡訊時會有兩個結果狀態,一個是簡訊是否成功發送,一個是對方是否成功接收,是否發送成功不是由Android程式來決定的,簡訊是由簡訊基站比如移動發送的,我們只需要把簡訊發送到移動無線通訊網路,網路傳送簡訊是否成功,移動會返回一個訊號,訊號會被程式捕獲。

  程式採用非同步方式捕獲訊號,因為不可能一直開著線程等待返回的訊號,所以sendTextMessage()的後兩個參數sentIntent和deliveryIntent就是接受發送和接收狀態訊號用的。

  sentIntent為簡訊發送是否成功的Intent;deliveryIntent為接收方是否收到了簡訊的Intent,這裡如果不想接收返回的訊號,可以設定為null。

  把Intent傳進去後,如果移動網路返回一個簡訊發送成功或失敗的訊號,作業系統會通過非同步方式廣播這個Intent,你就知道簡訊的狀態了。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.