Home>

Android sms messages are stored in the sqlite database in the android system.But do not let other programs access (Android system security mechanism)

Now we are reading the sms text messages in the mobile phone and first save them in the sqlite database defined by ourselvesThen read the sqlite database to extract the SMS,And show

sms SMS sqlite access code:

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;
/**
 * Read mobile text messages, Save to sqlite data first, and then read the database display
 *
 * @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 ();//create sqlite database
  smshander.insertsmstodatabase ();//read SMS
Insert into sqlite database
  cursor cursor=smshander.querysmsindatabase (100);//Get the first 100 messages (sorted by date)
  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);//bold font
   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);//create database
   db.execsql (sql);
  }
  //Get mobile message
  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");//Get SMS
   while (cursor.movetonext ()) {
    system.out.println ("-sms--:" + cursor.getstring (cursor.getcolumnindex ("body")));
   }
   return cursor;
  }
  //Save the phone message to sqlite database
  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 ();//Get SMS (cursor)
   db.begintransaction ();//start transaction
   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 ();//Set transaction successfully,Not set will automatically roll back without submitting
    db.endtransaction ();//End transaction
   }
  }
  //Get all SMS in sqlite database
  public cursor querysmsfromdatabase () {
   string sql="select * from sms order by date desc";
   return db.rawquery (sql, null);
  }
  //Get the latest size SMS in the sqlite database
  public cursor querysmsindatabase (int size) {
   string sql;
   cursor dbcount=db.rawquery ("select count (*) from sms", null);
   dbcount.movetofirst ();
   if (size<dbcount.getint (0)) {//If there are less than size SMS, then the previous size will be taken
    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);
  }
  //Get the first second SMS of the sqlite database
  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);
  }
  //close the database
  public void closesmsdatabase () {
   if (db!=null&&db.isopen ()) {
    db.close ();
    db=null;
   }
  }
 }
}

operation result:

  • Previous Java implementation of exporting a large number of data records from a database and saving them to a file
  • Next sqlserver rounding using round function and cast and convert functions