02发送短信

使用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"/>

*****************************
效果图:
打开两个虚拟机,输入虚拟机编号,输入短信内容,发送。
02发送短信_第1张图片


代码如下:

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;
	
	@Override
	protected 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);
		}
	}

}


代码下载


你可能感兴趣的:(02发送短信)