获取Android手机中所有短信的实现代码
作者:mdxy-dxy 发布时间:2023-08-04 16:55:30
标签:Android,短信
Java核心代码:
public String getSmsInPhone()
{
final String SMS_URI_ALL = "content://sms/";
final String SMS_URI_INBOX = "content://sms/inbox";
final String SMS_URI_SEND = "content://sms/sent";
final String SMS_URI_DRAFT = "content://sms/draft";
StringBuilder smsBuilder = new StringBuilder();
try{
ContentResolver cr = getContentResolver();
String[] projection = new String[]{"_id", "address", "person",
"body", "date", "type"};
Uri uri = Uri.parse(SMS_URI_ALL);
Cursor cur = cr.query(uri, projection, null, null, "date desc");
if (cur.moveToFirst()) {
String name;
String phoneNumber;
String smsbody;
String date;
String type;
int nameColumn = cur.getColumnIndex("person");
int phoneNumberColumn = cur.getColumnIndex("address");
int smsbodyColumn = cur.getColumnIndex("body");
int dateColumn = cur.getColumnIndex("date");
int typeColumn = cur.getColumnIndex("type");
do{
name = cur.getString(nameColumn);
phoneNumber = cur.getString(phoneNumberColumn);
smsbody = cur.getString(smsbodyColumn);
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");
Date d = new Date(Long.parseLong(cur.getString(dateColumn)));
date = dateFormat.format(d);
int typeId = cur.getInt(typeColumn);
if(typeId == 1){
type = "接收";
} else if(typeId == 2){
type = "发送";
} else {
type = "";
}
smsBuilder.append("[");
smsBuilder.append(name+",");
smsBuilder.append(phoneNumber+",");
smsBuilder.append(smsbody+",");
smsBuilder.append(date+",");
smsBuilder.append(type);
smsBuilder.append("] ");
if(smsbody == null) smsbody = "";
}while(cur.moveToNext());
} else {
smsBuilder.append("no result!");
}
smsBuilder.append("getSmsInPhone has executed!");
} catch(SQLiteException ex) {
Log.d("SQLiteException in getSmsInPhone", ex.getMessage());
}
return smsBuilder.toString();
}
注释:
1、本函数用于获取手机中所有的短信,包括收件箱、发件箱、草稿箱等。
2、本函数可以运行在Service子类中,因为未使用Activity类的相关函数。
3、获取的短信包括:收发短信人名、手机号码、短信内容、短信发送接收的时间、短信的类型。
sms主要结构:
_id:短信序号,如100
thread_id:对话的序号,如100,与同一个手机号互发的短信,其序号是相同的
address:发件人地址,即手机号,如+8613811810000
person:发件人,如果发件人在通讯录中则为具体姓名,陌生人为null
date:日期,long型,如1256539465022,可以对日期显示格式进行设置
protocol:协议0SMS_RPOTO短信,1MMS_PROTO彩信
read:是否阅读0未读,1已读
status:短信状态-1接收,0complete,64pending,128failed
type:短信类型1是接收到的,2是已发出
body:短信具体内容
service_center:短信服务中心号码编号,如+8613800755500
4、为了获取短信,需要在AndroidManifest.xml文件中添加权限使用说明,如下:
<uses-permissionandroid:name="android.permission.READ_SMS"/>
5、本函数在真机上测试通过。


猜你喜欢
- 最新Spring Data JPA官方参考手册 Version 2.0.0.RC2,2017-07-25https://docs.sprin
- 实践过程效果代码public partial class Form1 : Form {
- java Mybatis存进时间戳封装了一个实体类,里面有个字段 Integer createTime。要利用这个实体类将一个时间戳存进数据
- 一、树的概念和结构1.1 树的概念树是一种非线性的数据结构,它是由 n(n>=0)个有限结点组成一个具有层次关系的集合。把它叫做树是因
- 这篇文章主要介绍了Java利用读写的方式实现音频播放代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- Java中的try-catch-finally异常处理一、异常处理异常(Exception):是在运行发生的不正常情况。原始异常处理:if(
- 一、前期工作创建工作空间 二、创建工作包创建完成后,文件夹的格式为:三、准备编译文件和代码3.1 更换编译文件中的内容将上图中的,
- 上篇文章说了通过RestTemplate实现微服务之间访问:https://www.jb51.net/article/252981.htm,
- Spring-Context的作用spring-context提供应用程序上下文,这是Spring的依赖注入容器,它可能总是在以某种方式使用
- Lambda表达式类似匿名函数,简单地说,它是没有声明的方法,也即没有访问修饰符、返回值声明和方法名。Lambda允许把函数作为一个方法的参
- 之前在Spring Boot启动过程(二)提到过createEmbeddedServletContainer创建了内嵌的Servlet容器,
- 程序生成的自定义文件,比如后缀是.test这种文件怎么直接启动打开程序,并打开本文件呢 1、
- 本文实例为大家分享了java实现通过绑定邮箱找回密码功能,供大家参考,具体内容如下1.输入用户名及验证码,验证用户名是否存在(1).生成验证
- 现在以一个例子来介绍mybatis的动态SQL和模糊查询:通过多条件查询用户记录,条件为姓名模糊匹配,并且年龄在某两个值之间。新建表d_us
- 实践过程效果代码public partial class Form1 : Form{ public Form1()
- 好久没有写文章了,年前公司新开了一个项目,是和usb转串口通信相关的,需求是用安卓平板通过usb转接后与好几个外设进行通信,一直忙到最近,才
- Spring security记住我基本原理:登录的时候,请求发送给过滤器UsernamePasswordAuthenticationFil
- 前言在项目开发中经常会用到配置文件,配置文件的存在解决了很大一份重复的工作。今天就分享四种在Springboot中获取配置文件的方式。注:前
- 最近在玩3g体育门户客户端的时候,看到这样个效果: 轻触赛事图标,会有一个图标变大浮出的效果.,蛮有意思的.于是就把仿照它做了一
- 介绍众所周知,AOP(面向切面编程)是Spring框架的特色功能之一。通过设置横切关注点(cross cutting concerns),A