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 之 SMS 短信读取


更多相关文章

  1. SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
  2. android 实现服务器连接获取数据和传递数据(1)
  3. Android(安卓)系统各种音量的获取及音量的上调与下调
  4. Android基础(八):文件存储
  5. Android-广播接收者;短信监听
  6. android Spinner控件的使用
  7. android 安卓APP获取手机设备信息和手机号码的代码示例
  8. Android中获取TextView一行最多能显示几个字
  9. Android(安卓)联系人 数据库解析

随机推荐

  1. Android两条并排RecyclerView实时联动滑
  2. android客户端接入新浪、腾讯微博以及人
  3. Android中的WebView进行直接加载网页(要
  4. 谷歌:Android与Chrome OS最终将合而为一
  5. android EditText使用详解
  6. [置顶] hwui简介
  7. Android(安卓)Activity返回结果startActi
  8. NDK模块开发:关于音视频,你需要了解的东西
  9. Android 入坑之始
  10. tinyalsa 与 audioroute