Android短信發送功能實現技巧分享
編程人員可以對Android手機操作系統進行一些更改來滿足用戶的需求。不過要想對其進行修改,首先需要了解其源碼的編寫方式。在這里我們先來看看Android短信功能的具體實現,來體驗一下相關編寫方式。
1: Android短信發送可以在模擬器中進行模擬出來。
如現在啟動一模擬器id 號為5554,運行cmd
telnet localhost 5554
輸入help 可以看到很多用于模擬器中的功能命令
- gsm call 134343434
- // 便是呼叫當前模擬器命令
- sms send 15555218135 Hello,this is a Message
- // 是向當前的模擬器發送短信息
2: 相關類:
- Android.telephony.gsm.SmsManager
- Android.telephony.gsm.SmsMessage
- Android.app.PendingIntent
- Android.widget.Toast
3:Android短信發送實現代碼(節選)
- 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短信發送代碼解釋
上述發送短信的代碼很簡單,但是其中的幾個類函數并不好理解: #t#
Toast.makeText 就是展示一個提示信息,這個比較容易理解;
PendingIntent 就是一個Intent 的描述,我們可以把這個描述交給別的程序,別的程序
根據這個描述在后面的別的時間做你安排做的事情,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短信發送的相關實現方法就為大家介紹到這里。