天天看點

【Android】Android 發送短信和打電話的方法

有兩種方法可以實作發送短信,其一是使用intent-startActivity,URI資料格式為"smsto:num",調用的action為Intent.ACTION_SENDTO:

【Android】Android 發送短信和打電話的方法

其二是使用SmsManager:

【Android】Android 發送短信和打電話的方法

二者的不同在于前者隻是調用了發送界面,需要按下Send按鈕短信才發送出去,而後者則是直接發送出去。

發送SMS權限的設定:

<uses-permission android:name="android.permission.SEND_SMS"/>

關于SmsManager

SDK中的介紹:Manages SMS operations such as sending data, text, and pdu SMS

messages. Get this object by calling the static method

SmsManager.getDefault().

方法:

public void sendTextMessage (String destinationAddress, String

scAddress, String text, PendingIntent sentIntent, PendingIntent

deliveryIntent)

destinationAddress: 收件人位址

scAddress: 短信中心号碼,null為預設中心号碼

sentIntent:

當消息發出時,成功或者失敗的資訊報告通過PendingIntent來廣播。如果該參數為空,則發信程式會被所有位置程式檢查一遍,這樣會導緻發送時間延長。

deliveryIntent: 當消息發送到收件人時,該PendingIntent會被廣播。pdu資料在狀态報告的extended data

("pdu")中。

如果收件人或者資訊為空則抛出 IllegalArgumentException 。

public ArrayList<String> divideMessage (String

text)

将大于70字的短信分割為多條。

參數:text    the original message. Must not be

null.

傳回:an ArrayList of strings that, in order, comprise the original

message

sendDataMessage 參數與上類似,隻是用于發送Data。

sendMultipartTextMessage發送多條短信,發送内容必須是用divideMessage分割好了的。

 打電話的方法類似,所不用的是URI格式為"tel:num",而調用的action為Intent.ACTION_CALL:

打電話權限的設定:

【Android】Android 發送短信和打電話的方法

1.啟動android emulator,檢視标題欄找出端口。一般是android emulator

(5554),其中5554就是端口。

2.打開指令行,輸入telnet localhost 5554。程式将會連接配接到android console,傳回

Android Console: type ‘help‘ for a list of commands

OK

模拟電話打入gsm

<call|accept|busy|cancel|data|hold|list|voice|status>

輸入gsm call <模拟打進的電話号碼>。如:

gsm call 15555218135

模拟短信發送sms send <senderPhoneNumber> <textmessage>

輸入sms send <模拟發送短信的電話> <内容>。如:

sms send 15555218135 hello

其中,15555218135為模拟器手機号碼。