• Android--获取短信的内容,截取短信


    1、首先我们要写一个广播接收器,当我们的手机收到短信时,系统会自动发送一个广播,我们只需要接收到这条广播就可以了

    2、在广播里面,我们重写的onReceive()方法,通过里面的Intent写到的Bundle就可以拿到短信的内容,

    3、清单文件里面我们必须要添加权限,否则无法接收到。

    4、为了防止我们的广播接收不到,我们自己写的广播接收器的权限必须要大,以防万一,我设置了1000。

    下面上代码,里面的注释也比较详细..

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="com.example.fanlei.cutnotedemo" >
     4 
     5     //接收短信
     6     <uses-permission android:name="android.permission.RECEIVE_SMS"/>
     7     <application
     8         android:allowBackup="true"
     9         android:icon="@drawable/ic_launcher"
    10         android:label="@string/app_name"
    11         android:theme="@style/AppTheme" >
    12         <!-- action:name = 的名称是固定的 -->
    13         <receiver android:name=".NoteReceiver">
    14             <intent-filter android:priority="1000">
    15                 <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
    16             </intent-filter>
    17         </receiver>
    18         <activity
    19             android:name=".MainActivity"
    20             android:label="@string/app_name" >
    21             <intent-filter>
    22                 <action android:name="android.intent.action.MAIN" />
    23 
    24                 <category android:name="android.intent.category.LAUNCHER" />
    25             </intent-filter>
    26         </activity>
    27     </application>
    28 </manifest>

    写一个类,继承BroadcastReceiver

     1 package com.example.fanlei.cutnotedemo;
     2 
     3 import android.content.BroadcastReceiver;
     4 import android.content.Context;
     5 import android.content.Intent;
     6 import android.os.Bundle;
     7 import android.telephony.SmsMessage;
     8 import android.widget.Toast;
     9 
    10 import java.text.SimpleDateFormat;
    11 import java.util.Date;
    12 
    13 /**
    14  * 广播接收器
    15  */
    16 public class NoteReceiver extends BroadcastReceiver {
    17 
    18     private static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
    19     @Override
    20     public void onReceive(Context context, Intent intent) {
    21         String action = intent.getAction();
    22         //判断广播消息
    23         if (action.equals(SMS_RECEIVED_ACTION)){
    24             Bundle bundle = intent.getExtras();
    25             //如果不为空
    26             if (bundle != null){
    27                 //将pdus里面的内容转化成Object[]数组
    28                 Object pdusData[] = (Object[]) bundle.get("pdus");
    29                 //解析短信
    30                 SmsMessage[] msg = new SmsMessage[pdusData.length];
    31                 for (int i = 0;i < msg.length;i++){
    32                     byte pdus[] = (byte[]) pdusData[i];
    33                     msg[i] = SmsMessage.createFromPdu(pdus);
    34                 }
    35                 StringBuffer content = new StringBuffer();//获取短信内容
    36                 StringBuffer phoneNumber = new StringBuffer();//获取地址
    37                 StringBuffer receiveData = new StringBuffer();//获取时间
    38                 //分析短信具体参数
    39                 for (SmsMessage temp : msg){
    40                     content.append(temp.getMessageBody());
    41                     phoneNumber.append(temp.getOriginatingAddress());
    42                     receiveData.append(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS")
    43                             .format(new Date(temp.getTimestampMillis())));
    44                 }
    45                 /**
    46                  * 这里还可以进行好多操作,比如我们根据手机号进行拦截(取消广播继续传播)等等
    47                  */
    48                 Toast.makeText(context,phoneNumber.toString()+content+receiveData, Toast.LENGTH_LONG).show();//短信内容
    49             }
    50         }
    51     }
    52 }
  • 相关阅读:
    安装部署NetBeans mysql Tomact joget workflow 环境
    Django-分页扩展
    supervisor linux下进程管理工具
    注意python函数参数的可变变量的传递
    Python远程部署利器Fabric详解
    chrom 扩展程序安装
    supervisor
    python进度条
    os sys区别
    知乎上关于网站 权限系统的回答
  • 原文地址:https://www.cnblogs.com/819158327fan/p/4932199.html
Copyright © 2020-2023  润新知