Android 短信拦截(一)
本文转自http://www.cnblogs.com/GarfieldTom/archive/2012/08/25/2655609.html 基于4.03系统HTC测试可以正常拦截短信
要做一个自动短信回复,所以需要拦截系统短信.
1.在Manifest.xml里加"接收"SMS的权限
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
2.在Manifest.xml里注册一个receive
<!-- 注册Receiver,并且设置优先级 --> <receiver android:name=".AutoSMS" android:exported="false"> <intent-filter android:priority="1000"> <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver>
3.定义一个短信接收类,并且重写onReceive
//继承BroadcastReceiver public class AutoSMS extends BroadcastReceiver { private String TAG="AutSMS"; //广播消息类型 public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED"; //覆盖onReceive方法 @Override public void onReceive(Context context, Intent intent) {
下面是完整的代码:
Manifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xxh.autosms" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.RECEIVE_SMS"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 注册Receiver,并且设置优先级 --> <receiver android:name=".AutoSMS" android:exported="false"> <intent-filter android:priority="1000"> <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver> </application> </manifest>
AutoSMS.java:
package com.xxh.autosms; import java.text.SimpleDateFormat; import java.util.Date; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsMessage; import android.util.Log; import android.widget.Toast; //继承BroadcastReceiver public class AutoSMS extends BroadcastReceiver { private String TAG="AutSMS"; //广播消息类型 public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED"; //覆盖onReceive方法 @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Log.i(TAG, "引发接收事件"); //StringBuilder body=new StringBuilder("");//短信内容 //StringBuilder sender=new StringBuilder("");//发件人 //先判断广播消息 String action = intent.getAction(); if (SMS_RECEIVED_ACTION.equals(action)) { //获取intent参数 Bundle bundle=intent.getExtras(); //判断bundle内容 if (bundle!=null) { //取pdus内容,转换为Object[] Object[] pdus=(Object[])bundle.get("pdus"); //解析短信 SmsMessage[] messages = new SmsMessage[pdus.length]; for(int i=0;i<messages.length;i++) { byte[] pdu=(byte[])pdus[i]; messages[i]=SmsMessage.createFromPdu(pdu); } //解析完内容后分析具体参数 for(SmsMessage msg:messages) { //获取短信内容 String content=msg.getMessageBody(); String sender=msg.getOriginatingAddress(); Date date = new Date(msg.getTimestampMillis()); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String sendTime = sdf.format(date); //TODO:根据条件判断,然后进一般处理 if ("10060".equals(sender)) { // 屏蔽手机号为10060的短信,这里还可以时行一些处理,如把这个信息发送到第三人的手机等等。 //TODO:测试 Toast.makeText(context, "收到10060的短信"+"内容:"+content, Toast.LENGTH_LONG).show(); //对于特定的内容,取消广播 abortBroadcast(); } else { Toast.makeText(context, "收到:"+sender+"内容:"+content+"时间:"+sendTime.toString(), Toast.LENGTH_LONG).show(); } } } }//if 判断广播消息结束 } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端