Android编程之SMS读取短信并保存到SQLite的方法
作者:阳光岛主 时间:2022-01-30 18:02:45
本文实例讲述了Android编程之SMS读取短信并保存到SQLite的方法。分享给大家供大家参考,具体如下:
Android 之 SMS 短信在Android系统中是保存在SQLite数据库中的,但不让其它程序访问(Android系统的安全机制)
现在我们在读取手机内的SMS短信,先保存在我们自己定义的SQLite数据库中,然后读取SQLite数据库提取短信,并显示
SMS短信SQLite存取代码:
package com.homer.sms;
import java.sql.Date;
import java.text.SimpleDateFormat;
import org.loon.wsi.R;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;
/**
* 读取手机短信, 先保存到SQLite数据,然后再读取数据库显示
*
* @author sunboy_2050
* @since http://blog.csdn.net/sunboy_2050
* @date 2012.03.06
*/
public class smsRead4 extends Activity {
TableLayout tableLayout;
int index = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tableLayout = (TableLayout) findViewById(R.id.tableLayout);
showSMS();
}
private void showSMS() {
SmsHander smsHander = new SmsHander(this);
smsHander.createSMSDatabase(); // 创建SQLite数据库
smsHander.insertSMSToDatabase(); // 读取手机短信,插入SQLite数据库
Cursor cursor = smsHander.querySMSInDatabase(100); // 获取前100条短信(日期排序)
cursor.moveToPosition(-1);
while (cursor.moveToNext()) {
String strAddress = cursor.getString(cursor.getColumnIndex("address"));
String strDate = cursor.getString(cursor.getColumnIndex("date"));
String strBody = cursor.getString(cursor.getColumnIndex("body"));
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(Long.parseLong(strDate));
strDate = dateFormat.format(date);
String smsTitle = strAddress + "\t\t" + strDate;
String smsBody = strBody + "\n";
Log.i("tableRow", smsTitle + smsBody);
// title Row
TableRow trTitle = new TableRow(this);
trTitle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TextView tvTitle = new TextView(this);
tvTitle.setText(smsTitle);
tvTitle.getPaint().setFakeBoldText(true); // 加粗字体
tvTitle.setTextColor(Color.RED);
tvTitle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
trTitle.addView(tvTitle);
tableLayout.addView(trTitle, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// body Row
TableRow trBody = new TableRow(this);
trBody.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TextView tvBody = new TextView(this);
tvBody.setText(smsBody);
tvBody.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
trBody.addView(tvBody);
tableLayout.addView(trBody, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
if (!cursor.isClosed()) {
cursor.close();
cursor = null;
}
smsHander.closeSMSDatabase();
index = 0;
}
public class SmsHander {
SQLiteDatabase db;
Context context;
public SmsHander(Context context) {
this.context = context;
}
public void createSMSDatabase() {
String sql = "create table if not exists sms("
+ "_id integer primary key autoincrement,"
+ "address varchar(255)," + "person varchar(255),"
+ "body varchar(1024)," + "date varchar(255),"
+ "type integer)";
db = SQLiteDatabase.openOrCreateDatabase(context.getFilesDir().toString() + "/data.db3", null); // 创建数据库
db.execSQL(sql);
}
// 获取手机短信
private Cursor getSMSInPhone() {
Uri SMS_CONTENT = Uri.parse("content://sms/");
String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };
Cursor cursor = context.getContentResolver().query(SMS_CONTENT, projection, null, null, "date desc"); // 获取手机短信
while (cursor.moveToNext()) {
System.out.println("--sms-- : " + cursor.getString(cursor.getColumnIndex("body")));
}
return cursor;
}
// 保存手机短信到 SQLite 数据库
public void insertSMSToDatabase() {
Long lastTime;
Cursor dbCount = db.rawQuery("select count(*) from sms", null);
dbCount.moveToFirst();
if (dbCount.getInt(0) > 0) {
Cursor dbcur = db.rawQuery("select * from sms order by date desc limit 1", null);
dbcur.moveToFirst();
lastTime = Long.parseLong(dbcur.getString(dbcur.getColumnIndex("date")));
} else {
lastTime = new Long(0);
}
dbCount.close();
dbCount = null;
Cursor cur = getSMSInPhone(); // 获取短信(游标)
db.beginTransaction(); // 开始事务处理
if (cur.moveToFirst()) {
String address;
String person;
String body;
String date;
int type;
int iAddress = cur.getColumnIndex("address");
int iPerson = cur.getColumnIndex("person");
int iBody = cur.getColumnIndex("body");
int iDate = cur.getColumnIndex("date");
int iType = cur.getColumnIndex("type");
do {
address = cur.getString(iAddress);
person = cur.getString(iPerson);
body = cur.getString(iBody);
date = cur.getString(iDate);
type = cur.getInt(iType);
if (Long.parseLong(date) > lastTime) {
String sql = "insert into sms values(null, ?, ?, ?, ?, ?)";
Object[] bindArgs = new Object[] { address, person, body, date, type };
db.execSQL(sql, bindArgs);
} else {
break;
}
} while (cur.moveToNext());
cur.close();
cur = null;
db.setTransactionSuccessful(); // 设置事务处理成功,不设置会自动回滚不提交
db.endTransaction(); // 结束事务处理
}
}
// 获取 SQLite 数据库中的全部短信
public Cursor querySMSFromDatabase() {
String sql = "select * from sms order by date desc";
return db.rawQuery(sql, null);
}
// 获取 SQLite 数据库中的最新 size 条短信
public Cursor querySMSInDatabase(int size) {
String sql;
Cursor dbCount = db.rawQuery("select count(*) from sms", null);
dbCount.moveToFirst();
if (size < dbCount.getInt(0)) { // 不足 size 条短信,则取前 size 条
sql = "select * from sms order by date desc limit " + size;
} else {
sql = "select * from sms order by date desc";
}
dbCount.close();
dbCount = null;
return db.rawQuery(sql, null);
}
// 获取 SQLite数据库的前 second秒短信
public Cursor getSMSInDatabaseFrom(long second) {
long time = System.currentTimeMillis() / 1000 - second;
String sql = "select * from sms order by date desc where date > " + time;
return db.rawQuery(sql, null);
}
// 关闭数据库
public void closeSMSDatabase() {
if (db != null && db.isOpen()) {
db.close();
db = null;
}
}
}
}
运行结果:
完整实例代码代码点击此处本站下载。
希望本文所述对大家Android程序设计有所帮助。
标签:Android,短信,SQLite
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Android 进程
2023-04-26 07:37:55
![](https://img.aspxhome.com/file/2023/8/138678_0s.png)
springcloud使用feign调用服务时参数内容过大问题
2022-08-31 11:23:26
![](https://img.aspxhome.com/file/2023/8/106118_0s.png)
完美解决虚拟按键遮盖底部视图的问题
2021-11-24 23:48:25
C#的并发机制优秀在哪你知道么
2022-11-09 09:44:35
![](https://img.aspxhome.com/file/2023/1/116651_0s.png)
Android中删除Preference详解
2021-08-09 21:41:19
![](https://img.aspxhome.com/file/2023/2/138192_0s.png)
opencv3/C++ FLANN特征匹配方式
2021-08-19 10:17:47
![](https://img.aspxhome.com/file/2023/7/117777_0s.jpg)
Android制作漂亮自适布局键盘的方法
2022-08-04 09:14:53
![](https://img.aspxhome.com/file/2023/3/137933_0s.png)
springboot aop配合反射统一签名验证实践
2023-08-04 08:14:07
![](https://img.aspxhome.com/file/2023/7/76257_0s.png)
Java经典用法总结(二)
2023-11-24 20:39:10
Java编程实现轨迹压缩算法开放窗口实例代码
2021-12-03 09:54:07
快速学习六大排序算法
2023-11-02 22:36:19
![](https://img.aspxhome.com/file/2023/5/105175_0s.gif)
Spring中多配置文件及引用其他bean的方式
2023-07-01 17:31:03
![](https://img.aspxhome.com/file/2023/5/62715_0s.jpg)
C#实现无损压缩图片的示例详解
2023-04-29 21:07:26
![](https://img.aspxhome.com/file/2023/2/84442_0s.png)
c# 如何实现代码生成器
2023-11-13 19:23:35
![](https://img.aspxhome.com/file/2023/0/94540_0s.png)
Android提醒微技巧你真的了解Dialog、Toast和Snackbar吗
2023-03-08 14:15:44
![](https://img.aspxhome.com/file/2023/0/103890_0s.jpg)
SpringBoot项目实战之加载和读取资源文件
2023-10-07 06:00:41
Android应用创建桌面快捷方式代码
2022-03-03 00:51:44
C#缓存之SqlCacheDependency用法实例总结
2023-04-14 10:31:37
如何调用chatGPT实现代码机器人
2023-06-05 02:09:33
![](https://img.aspxhome.com/file/2023/0/104960_0s.jpg)
基于String和List<String>间的相互转换方式
2022-09-25 15:52:21
![](https://img.aspxhome.com/file/2023/0/116180_0s.png)