Android 读取短信内容(模拟器)
读取短信内容
读取短信的内容就要知道短信放在哪里,然后通过查询语句把结果封装到实体类的List中,再全部打印出来即可。
下面是具体的代码:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg"android:orientation="vertical"tools:context=".MainActivity"><TextViewandroid:id="@+id/tv_des"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="18dp"android:paddingLeft="20dp"android:text="读取到的系统短信如下:"android:textSize="20sp"android:visibility="invisible"></TextView><TextViewandroid:id="@+id/tv_sms"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/tv_des"android:layout_marginTop="0dp"android:lines="20"android:paddingLeft="20dp"android:paddingTop="10dp"android:textSize="16sp"></TextView><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"android:layout_marginBottom="28dp"android:layout_marginLeft="26dp"android:layout_marginStart="26dp"android:background="#149EDD"android:onClick="readSms"android:padding="5dp"android:text="查看短信"android:textSize="30sp"></Button></RelativeLayout>
MainActivity.java
package com.example.readsms;import androidx.appcompat.app.AppCompatActivity;import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;import java.util.ArrayList;
import java.util.List;public class MainActivity extends AppCompatActivity {private TextView tvSms;private TextView tvDes;private String text;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tvSms = (TextView)findViewById(R.id.tv_sms) ;tvDes = (TextView)findViewById(R.id.tv_des) ;}public void readSms(View view){//查询短信的uriUri uri = Uri.parse("content://sms/");//获取ContentResolver对象ContentResolver resolver = getContentResolver();Cursor cursor = resolver.query(uri,new String[]{"_id","address","type","body","date"},null,null,null);List<SmsInfo> smsInfos = new ArrayList<SmsInfo>();if (cursor != null && cursor.getCount()>0){tvDes.setVisibility(View.VISIBLE);while(cursor.moveToNext()){int _id = cursor.getInt(0);String address =cursor.getString(1);int type = cursor.getInt(2);String body = cursor.getString(3);long date = cursor.getLong(4);SmsInfo smsInfo = new SmsInfo(_id,address,type,body,date);smsInfos.add(smsInfo);}cursor.close();}for (int i=0;i<smsInfos.size();i++){text += "手机号码" + smsInfos.get(i).getAddress()+"\n";text += "短信内容" + smsInfos.get(i).getBody()+"\n";tvSms.setText(text);}}
}
SmsInfo.java
package com.example.readsms;public class SmsInfo {private int _id; //短信主键private String address; //发送地址private int type; //类型private String body; //短信内容private long date; //时间public SmsInfo(int _id, String address, int type, String body, long date) {this._id = _id;this.address = address;this.type = type;this.body = body;this.date = date;}public int get_id() {return _id;}public void set_id(int _id) {this._id = _id;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public int getType() {return type;}public void setType(int type) {this.type = type;}public String getBody() {return body;}public void setBody(String body) {this.body = body;}public long getDate() {return date;}public void setDate(long date) {this.date = date;}@Overridepublic String toString() {return "SmsInfo{" +"_id=" + _id +", address='" + address + '\'' +", type=" + type +", body='" + body + '\'' +", date=" + date +'}';}
}
Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.readsms"><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission><uses-permission android:name="android.permission.READ_SMS"></uses-permission><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>
运行截图:
与短信内容一致。
Android 读取短信内容(模拟器)相关推荐
- androidの读取短信内容
androidの读取短信内容 1. 读取短信内容,很简单,直接上代码 <span style="font-size:14px;"> public Uri SMS_INB ...
- android读取短信内容简书,Android 获取App信息
首先,来个简单的,获取本应用的信息 : //获取 build.gradle 中的 versionName 和 versionCode try { PackageInfo info = getPacka ...
- Android接收读取短信内容
一.注册短信接收广播: public static final String SMS_BORADCAST = "android.provider.Telephony.SMS_RECEIVED ...
- JavaScript 通过plus读取android手机短信内容
JavaScript 通过plus读取android手机短信内容. 1.获取android权限 在HBuilder工程中双击应用的"manifest.json"文件,切换到&quo ...
- Android读取短信和联系人
读取短信和联系人经常会用到,要了解的是这是内容提供者(contentProvider)的知识点,大家都知道数据库是在data-->data 对应的包目录下,其他应用是不可以访问到的,如果有些数据 ...
- android修改短信内容,Android手机
Android手机为例,如何查看/更改短信中心号码 在上述的几个情况之中,第一种现象比较普遍.许多用户接收不到信息大多也和短信中心号码有关.在Symbian系统手机,以及Windows Mobile系 ...
- android 读取短信 彩信,Android 短信转换成彩信的消息数量(实例代码)
默认3条以上转为彩信 改为5条 路径vendor/mediatek/proprietary/packages/apps/Mms/src/com/android/mms/MmsConfig.java p ...
- android 读取短信 彩信,Android系统中发送短信和彩信的代码
发送短信: String body="this is sms demo"; Intent mmsintent = new Intent(Intent.ACTION_SENDTO, ...
- Android开发短信拦截与读取,消息通知Notification
截获系统广播,读取消息,若联系人存在于通信录中,则让消息继续传播:若属于陌生号码,则中断消息,同时把信息显示在自己的程序中. 拦截器代码如下: public class SMSInterceptRec ...
最新文章
- ERROR LNK2019:无法解析的外部的符号 _sscanf或者_vsprintf
- linux服务安装框架,CentOS7安装经典Web架构:LAMP
- Windows Phone 7 程序菜单栏ApplicationBar
- php+header+跳转输出,php利用header跳转怎么失效了?
- php截取一个中文字符,php中的一个中文字符串截取函数
- 利用VB.NET绘制简易表白小程序
- spring mvc 自定义编辑器
- access数据库驱动的安装方法
- 植被抽稀lisp程序_地形图缩编中植被符号抽稀方法.doc
- 软件研发成本构成中的间接成本包括哪些?
- 猫怎么设置虚拟服务器,光纤猫设置虚拟服务器
- coodblock调试_codeblocks怎么调试?
- Java并发编程--线程池ThreadPollExecutor原理探究
- 几大搜索引擎搜索结果页大小的比较
- adblock 屏蔽网站广告 免费
- FPGA|VGA的8色彩条的实现
- Evaluate之迷思
- 微信公众号认证全过程
- Python温习(六)——元组操作
- mysql preparing状态_【Docker】在集群中部署应用为什么始终是Preparing状态