Android中实现基本的短信拦截功能的代码示例
程序员文章站
2024-02-28 20:08:34
要点
1.在manifest.xml里加"接收"sms的权限
要点
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 判断广播消息结束 } }
下一篇: 浅析java创建文件和目录