新聞中心
編程人員可以對Android手機(jī)操作系統(tǒng)進(jìn)行一些更改來滿足用戶的需求。不過要想對其進(jìn)行修改,首先需要了解其源碼的編寫方式。在這里我們先來看看Android短信功能的具體實(shí)現(xiàn),來體驗(yàn)一下相關(guān)編寫方式。

1: Android短信發(fā)送可以在模擬器中進(jìn)行模擬出來。
如現(xiàn)在啟動(dòng)一模擬器id 號為5554,運(yùn)行cmd
telnet localhost 5554
輸入help 可以看到很多用于模擬器中的功能命令
- gsm call 134343434
- // 便是呼叫當(dāng)前模擬器命令
- sms send 15555218135 Hello,this is a Message
- // 是向當(dāng)前的模擬器發(fā)送短信息
2: 相關(guān)類:
- Android.telephony.gsm.SmsManager
- Android.telephony.gsm.SmsMessage
- Android.app.PendingIntent
- Android.widget.Toast
3:Android短信發(fā)送實(shí)現(xiàn)代碼(節(jié)選)
- String msg ="hello";
- string number = "1234565678";
- SmsManager sms = SmsManager.getDefault();
- PendingIntent pi = PendingIntent.
getBroadcast(Sms.this,0,new Intent(),0);- sms.sendTextMessage(number,null,msg,pi,null);
- Toast.makeText(Sms.this,"send success",
Toast.LENGHT_LONG).show();
4:Android短信發(fā)送代碼解釋
上述發(fā)送短信的代碼很簡單,但是其中的幾個(gè)類函數(shù)并不好理解: #t#
Toast.makeText 就是展示一個(gè)提示信息,這個(gè)比較容易理解;
PendingIntent 就是一個(gè)Intent 的描述,我們可以把這個(gè)描述交給別的程序,別的程序
根據(jù)這個(gè)描述在后面的別的時(shí)間做你安排做的事情,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,就相當(dāng)于你的代表了。本例中別的程序就是發(fā)送短信的程序,短信發(fā)送成功后要把intent 廣播出去 。
函數(shù)sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)
前三個(gè)參數(shù)按照文檔比較容易理解,PendingIntent sentIntent 當(dāng)短信發(fā)出時(shí),成功的話sendIntent會(huì)把其內(nèi)部的描述的intent廣播出去,否則產(chǎn)生錯(cuò)誤代碼并通過Android.app.PendingIntent.OnFinished進(jìn)行回調(diào),這個(gè)參數(shù)***不為空,否則會(huì)存在資源浪費(fèi)的潛在問題;
deliveryIntent 是當(dāng)消息已經(jīng)傳遞給收信人后所進(jìn)行的PendingIntent 廣播。
查看PendingIntent 類可以看到許多的Send函數(shù),就是PendingIntent在進(jìn)行被賦予的相關(guān)的操作。
Android短信發(fā)送的相關(guān)實(shí)現(xiàn)方法就為大家介紹到這里。
標(biāo)題名稱:Android短信發(fā)送功能實(shí)現(xiàn)技巧分享
路徑分享:http://m.5511xx.com/article/cdjpgpc.html


咨詢
建站咨詢
