欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android编程之SMS读取短信并保存到SQLite的方法

程序员文章站 2023-12-12 18:27:16
本文实例讲述了android编程之sms读取短信并保存到sqlite的方法。分享给大家供大家参考,具体如下: android 之 sms 短信在android系统中是保存...

本文实例讲述了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编程之SMS读取短信并保存到SQLite的方法

完整实例代码代码点击此处本站下载

希望本文所述对大家android程序设计有所帮助。

上一篇:

下一篇: