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 判断广播消息结束
}
}
分享到:
相关推荐
Android4.4+短信拦截删除,Android4.4或者更高版本短信拦截与删除。本人在Android4.4.4红米上测试通过
Android短信拦截Android短信拦截Android短信拦截Android短信拦截Android短信拦截
android 短信电话拦截 广播 service menutable
android电话和短信拦截
Android 短信拦截器!包含代码。 环境:android 4.0.3
Android 实现来电拦截,
Android开发学习,有序广播拦截短信
用android编写的短信和电话的拦截,导入到eclipse中可以直接运行
进行过研究,也拦截成功了。 但是针对不同的手机厂家的android系统(略有不同),有的拦截就不成功了。 一般情况下,使用较标准的Google的android系统,拦截比较...会出现短信进入收件箱,才删除,或者根本拦截不成功。
Android拦截接收短信
android实现短信拦截的小例子,通过BroadcastReceiver
android 短信电话拦截 menutable service
利用广播进行过滤短信,可以将你需要加密的短信显示到这个app上,而不会在显示到系统自带的短信中
android 短信 发送 监听 拦截等自己写的demo
指定号码短信拦截源码可以拦截指定号码的短信,让短信不显示。
自己写android短信拦截 可做参考 希望对你有帮助
包括Android 移动开发基础案例教程课本中第六章实战演练——拦截史迪仔电话的源代码,可用Android studio运行,适合初学者学习。
Android实现黑名单拦截+通话双向录音 [注:本内容来自网络,在此分享仅为帮助有需要的网友,如果侵犯了您的权利,麻烦联系我,我会第一时间删除,谢谢您。]
Android中采用广播接收者实现短信监控器、拦截外拨电话功能案例!