iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android中实现基本的短信拦截功能的代码示例
  • 749
分享到

Android中实现基本的短信拦截功能的代码示例

示例Android 2022-06-06 08:06:34 749人浏览 八月长安
摘要

要点 1.在Manifest.xml里加"接收"SMS的权限 <uses-permission Android:name="android.permission.REC

要点

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 判断广播消息结束 
  } 
} 
您可能感兴趣的文章:android电话窃听器(示例代码)Android编程之短信窃听器实现方法Android 实现电话拦截及拦截提示音功能的开发Android广播接实现监听电话状态(电话的状态,拦截)Android开发四大组件之实现电话拦截和电话录音Android拦截外拨电话程序示例Android实现短信验证码自动拦截读取功能Android编程实现拦截短信并屏蔽系统Notification的方法android短信拦截的实现代码Android开发实现的电话窃听和拦截应用


--结束END--

本文标题: Android中实现基本的短信拦截功能的代码示例

本文链接: https://www.lsjlt.com/news/25155.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作