Android短信發送功能實現技巧分享

編程人員能夠對Android手機操做系統進行一些更改來知足用戶的需求。不過要想對其進行修改,首先須要瞭解其源碼的編寫方式。在這裏咱們先來看看Android短信功能的具體實現,來體驗一下相關編寫方式。 html

1: Android短信發送能夠在模擬器中進行模擬出來。  編程

如如今啓動一模擬器id 號爲5554,運行cmd 
telnet localhost 5554 網絡

輸入help 能夠看到不少用於模擬器中的功能命令 app

 
  1. gsm call 134343434   // 即是呼叫當前模擬器命令   
  2. sms send 15555218135 Hello,this is a Message   // 是向當前的模擬器發送短信息  

2: 相關類:  函數

 
  1. Android.telephony.gsm.SmsManager   Android.telephony.gsm.SmsMessage   
  2. Android.app.PendingIntent   Android.widget.Toast  


3:Android短信發送實現代碼(節選)  this

 
  1. String msg ="hello";   string number = "1234565678";   
  2. SmsManager sms = SmsManager.getDefault();   PendingIntent pi = PendingIntent. getBroadcast(Sms.this,0,new Intent(),0);   
  3. sms.sendTextMessage(number,null,msg,pi,null);   Toast.makeText(Sms.this,"send success", Toast.LENGHT_LONG).show();  


4:Android短信發送代碼解釋  操作系統

上述發送短信的代碼很簡單,可是其中的幾個類函數並很差理解: Android重力感應實現方式簡介Android Theme詳細內容概述Android應用技巧總結Android顯示網絡圖片相關實現方法淺談Android開機自啓動具體操做方法簡介 code

Toast.makeText 就是展現一個提示信息,這個比較容易理解; orm

PendingIntent 就是一個Intent 的描述,咱們能夠把這個描述交給別的程序,別的程序 htm

根據這個描述在後面的別的時間作你安排作的事情,By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other 
application was yourself,就至關於你的表明了。本例中別的程序就是發送短信的程序,短信發送成功後要把intent 廣播出去 。

函數sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)

前三個參數按照文檔比較容易理解,PendingIntent sentIntent 當短信發出時,成功的話sendIntent會把其內部的描述的intent廣播出去,不然產生錯誤代碼並經過Android.app.PendingIntent.OnFinished進行回調,這個參數最好不爲空,不然會存在資源浪費的潛在問題;

deliveryIntent 是當消息已經傳遞給收信人後所進行的PendingIntent 廣播。

查看PendingIntent 類能夠看到許多的Send函數,就是PendingIntent在進行被賦予的相關的操做。

Android短信發送的相關實現方法就爲你們介紹到這裏。

原文:http://www.baisoujs.com/detail_137070713892117.html

相關文章
相關標籤/搜索