android短信系列之实现发送短信,并获得发送报告与接收报告
首先写两个广播接收类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<String> list;
private String mesgstr;
/**发送与接收的广播**/
String SENT_SMS_ACTION = "SENT_SMS_ACTION";
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
public MsgSentManager(Context context, ArrayList<String> phonenos,
String msgstr) {
c = context;
list = phonenos;
this.mesgstr = msgstr;
}
public boolean sendMsgs(Context context, ArrayList<String> 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);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述