使用SmsManager發送短信
java.lang.Object | |
???? | android.telephony.SmsManager |
Manages SMS operations such as sending data, text, and pdu SMS messages. Get this object by calling the static method SmsManager.getDefault().
管理短信操作,如發送數據,文本和PDU短信。通過調用靜態方法SmsManager.getDefault()?獲取此對象。
使用到的API:
1、public void sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)
Parameters
destinationAddress | the address to send the message to ?? 消息的目標地址,短信的接收方 |
---|---|
scAddress | is the service center address or null to use the current default SMSC? 短信服務中心的地址,or 為空使用當前默認的 SMSC,一般用null |
text | the body of the message to send 短信內容 |
sentIntent | if not NULL this PendingIntent is broadcast when the message is successfully sent, or failed. The result code will beActivity.RESULT_OK for success, or one of these errors:RESULT_ERROR_GENERIC_FAILURE RESULT_ERROR_RADIO_OFF RESULT_ERROR_NULL_PDU For RESULT_ERROR_GENERIC_FAILURE the sentIntent may include the extra "errorCode" containing a radio technology specific value, generally only useful for troubleshooting.The per-application based SMS control checks sentIntent. If sentIntent is NULL the caller will be checked against all unknown applications, which cause smaller number of SMS to be sent in checking period. 不為null的話,則PendingIntent會發送廣播: Activity.RESULT_OK ?成功RESULT_ERROR_GENERIC_FAILURE?不確定的錯誤? RESULT_ERROR_RADIO_OFF radio關閉 RESULT_ERROR_NULL_PDU PDU錯誤 |
deliveryIntent | if not NULL this PendingIntent is broadcast when the message is delivered to the recipient. The raw pdu of the status report is in the extended data ("pdu").如果不為空,當消息成功傳送到接收者這個?PendingIntent?就廣播。? |
Throws
IllegalArgumentException | if destinationAddress or text are empty? |
---|
2、public ArrayList<String>divideMessage(String text)
Divide a message text into several fragments, none bigger than the maximum SMS message size.
3、不能忘記配置權限
<uses-permission android:name="android.permission.SEND_SMS"/>
*****************************
效果圖:
打開兩個虛擬機,輸入虛擬機編號,輸入短信內容,發送。
代碼如下:
MainActivity.java
package com.example.sendMsg;import java.util.ArrayList;
import java.util.List;import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;public class MainActivity extends Activity {private EditText ed_phone;private EditText ed_content;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ed_phone=(EditText) findViewById(R.id.et_phone);ed_content=(EditText) findViewById(R.id.et_content);}public void sendMessage(View v){String phoneNumber=ed_phone.getText().toString();String content=ed_content.getText().toString();//獲取短信管理器SmsManager sm=SmsManager.getDefault();//切割短信,把長短信切割成多條短信List<String> msgList=new ArrayList<String>();msgList=sm.divideMessage(content);//發送短信for(String msg:msgList){sm.sendTextMessage(phoneNumber, null, msg, null, null);}}}
代碼下載