Android短信發(fā)送功能實現(xiàn)技巧分享
編程人員可以對Android手機操作系統(tǒng)進行一些更改來滿足用戶的需求。不過要想對其進行修改,首先需要了解其源碼的編寫方式。在這里我們先來看看Android短信功能的具體實現(xiàn),來體驗一下相關(guān)編寫方式。
1: Android短信發(fā)送可以在模擬器中進行模擬出來。
如現(xiàn)在啟動一模擬器id 號為5554,運行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ā)送實現(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ā)送短信的代碼很簡單,但是其中的幾個類函數(shù)并不好理解: #t#
Toast.makeText 就是展示一個提示信息,這個比較容易理解;
PendingIntent 就是一個Intent 的描述,我們可以把這個描述交給別的程序,別的程序
根據(jù)這個描述在后面的別的時間做你安排做的事情,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)
前三個參數(shù)按照文檔比較容易理解,PendingIntent sentIntent 當(dāng)短信發(fā)出時,成功的話sendIntent會把其內(nèi)部的描述的intent廣播出去,否則產(chǎn)生錯誤代碼并通過Android.app.PendingIntent.OnFinished進行回調(diào),這個參數(shù)***不為空,否則會存在資源浪費的潛在問題;
deliveryIntent 是當(dāng)消息已經(jīng)傳遞給收信人后所進行的PendingIntent 廣播。
查看PendingIntent 類可以看到許多的Send函數(shù),就是PendingIntent在進行被賦予的相關(guān)的操作。
Android短信發(fā)送的相關(guān)實現(xiàn)方法就為大家介紹到這里。