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

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

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

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

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

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fanlei.cutnotedemo" > //接收短信
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- action:name = 的名称是固定的 -->
<receiver android:name=".NoteReceiver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

写一个类,继承BroadcastReceiver

 package com.example.fanlei.cutnotedemo;

 import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast; import java.text.SimpleDateFormat;
import java.util.Date; /**
* 广播接收器
*/
public class NoteReceiver extends BroadcastReceiver { private static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//判断广播消息
if (action.equals(SMS_RECEIVED_ACTION)){
Bundle bundle = intent.getExtras();
//如果不为空
if (bundle != null){
//将pdus里面的内容转化成Object[]数组
Object pdusData[] = (Object[]) bundle.get("pdus");
//解析短信
SmsMessage[] msg = new SmsMessage[pdusData.length];
for (int i = 0;i < msg.length;i++){
byte pdus[] = (byte[]) pdusData[i];
msg[i] = SmsMessage.createFromPdu(pdus);
}
StringBuffer content = new StringBuffer();//获取短信内容
StringBuffer phoneNumber = new StringBuffer();//获取地址
StringBuffer receiveData = new StringBuffer();//获取时间
//分析短信具体参数
for (SmsMessage temp : msg){
content.append(temp.getMessageBody());
phoneNumber.append(temp.getOriginatingAddress());
receiveData.append(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS")
.format(new Date(temp.getTimestampMillis())));
}
/**
* 这里还可以进行好多操作,比如我们根据手机号进行拦截(取消广播继续传播)等等
*/
Toast.makeText(context,phoneNumber.toString()+content+receiveData, Toast.LENGTH_LONG).show();//短信内容
}
}
}
}

最新文章

  1. crontab每秒执行URL接口
  2. 一个简单的左侧固定右侧自适应demo
  3. [mk] 喝一杯咖啡, 写一写 Makefile
  4. ASP.NET MVC view引入命名空间
  5. kali 虚拟机开启mysql:3306 供主机访问.
  6. 纯CSS制作三角(转)
  7. Javascript中那些偏门的知识
  8. 【web开发学习笔记】ibatis学习总结
  9. iOS 10 个实用小技巧(总有你不知道的和你会用到的)
  10. Nubia Z5S 基于官方H207/4.4内核的Mokee4.4.4 RC3.2 (2014.7.31修复呼吸灯(能亮依旧不能呼吸))
  11. PopupWindow的基本使用
  12. css动画过渡
  13. Android安全专项-利用androguard分析微信
  14. “net usershare”返回错误 255
  15. NodeJS+Express+MySQL开发小记(2):服务器部署
  16. SSM + Android 网络文件上传下载
  17. 听 Fabien Potencier 谈Symfony2 之 《What is Symfony2 ?》
  18. unity 2048Game
  19. 2018 Multi-University Training Contest 4 Problem B. Harvest of Apples 【莫队+排列组合+逆元预处理技巧】
  20. hdu 1428(很好的一道题,最短路+记忆化搜索)

热门文章

  1. 慕课网-安卓工程师初养成-1-3 使用记事本编写Java程序
  2. Weblogic发布小问题——The root element weblogic-web-app is missing in the descriptor file
  3. WF4.0 自定义CodeActivity与Bookmark&lt;第三篇&gt;
  4. Delphi Form的释放和隐藏:free,hide,close
  5. css实现div悬浮层,始终停留在浏览器的最下方,不随页面的滚动条滚动改变位置或消失
  6. JS常用的设计模式(13)——组合模式
  7. angular service讲解
  8. 图解Javascript上下文与作用域
  9. 001Linux命令
  10. sqlserver监控阻塞(死锁)具体情况