首先写两个广播接收类SendReceive和DeliverReceive
package com.chaowen.service;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class SendReceive extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //判断短信是否发送成功 switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(context, "短信发送成功", Toast.LENGTH_SHORT).show(); break; default: Toast.makeText(context, "发送失败", Toast.LENGTH_LONG).show(); break; } }}
package com.chaowen.service;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class DeliverReceive extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //表示对方成功收到短信 Toast.makeText(context, "对方接收成功",Toast.LENGTH_LONG).show(); }}
二.在代码中注册这两个类。
/**发送与接收的广播**/
String SENT_SMS_ACTION = "SENT_SMS_ACTION";
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
//注册一个短信接受报告
deliverReceive = new DeliverReceive();
sendReceive = new SendReceive();
IntentFilter sendFilter = new IntentFilter();
sendFilter.addAction(SENT_SMS_ACTION);
IntentFilter deliverFilter = new IntentFilter();
deliverFilter.addAction(DELIVERED_SMS_ACTION);
this.registerReceiver(deliverReceive, deliverFilter);
this.registerReceiver(sendReceive, sendFilter);
三.调用以下工具类发送短信
package com.chaowen.util;import java.util.ArrayList;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.telephony.gsm.SmsManager;import android.util.Log;//发送读信息的类 类中提供 发送消息给联系人的的方法public class MsgSentManager implements Runnable { private Context c; private ArrayList list; private String mesgstr; /**发送与接收的广播**/ String SENT_SMS_ACTION = "SENT_SMS_ACTION"; String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION"; public MsgSentManager(Context context, ArrayList phonenos, String msgstr) { c = context; list = phonenos; this.mesgstr = msgstr; } public boolean sendMsgs(Context context, ArrayList phonenos, String msgstr) { for (String str : phonenos) { sendOneMsg(context, str, msgstr); } return true; } public boolean sendOneMsg(Context context, String phoneno, String msgstr) { // 手机发送短消息的功能实现: // 同样需要在androidmanifest。Xml文件中定义 users-permisssion // 这里要写 name=android.permission.SEND_SMS // 先建构一PendingIntent对象并使用getBroadcast()广播将PendingIntent,电话,短信文字等参数传入sendTextMessage()方法发送短信 //下面的mPI为了获得发送报告的,发送报告,只是短信发送出去,对面是否接受不关心 SmsManager smsManager = SmsManager.getDefault(); PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent(SENT_SMS_ACTION), 0); // DeliverPI为了获得对方接受到之后返回的报告的 //接收报告:就是发送方的短信发送到对方手机上之后,对方手机会返回给运营商一个信号,告知运营商收到短信,运营商再把这个信号发给发送方,发送方得到这个信号之后, Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION); PendingIntent deliverPI = PendingIntent.getBroadcast(context, 0, deliverIntent, 0); smsManager.sendTextMessage(phoneno, null, msgstr, sentPI , deliverPI); Log.i("sendmsg", "i have been send the msg."); // sendTextMessage ()的参数 意义依次为 : // 1 短信发送的地址也就是电话号码 // 2 sc地址 // 3 短消息的内容 // 4pendingintent 参数 // 5 发送intent return true; } public void run() { // TODO Auto-generated method stub sendMsgs(c, list, mesgstr); }}
来源:https://www.cnblogs.com/andgoo/archive/2011/10/18/2216782.html