Android programming SMS Read SMS and save to SQLite method _android

Source: Internet
Author: User
Tags dateformat idate int size sqlite sqlite database create database

This article is an example of how Android programming SMS reads SMS and saves it to SQLite. Share to everyone for your reference, specific as follows:

Android SMS messages are kept in the SQLite database in the Android system, but are not accessible to other programs (Android security)

Now we're reading SMS messages in our phones, saving them in our own defined SQLite database, and then reading the SQLite database to extract text messages and display

SMS Message 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, first save to SQLite data, 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 cell phone SMS, insert SQLite database Cursor Cursor = smshander.querysmsindatabase (100); 
  Get the first 100 SMS (date Sort) 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 {this.context = context;  public void Createsmsdatabase () {String sql = ' CREATE table if not exists SMS (' + ' _id integer primary key AutoIncrement, "+" address varchar (255), "+" who varchar (255), "+" body varchar (1024), "+" date varchar (25 
   5), "+" type integer); db = Sqlitedatabase.openorcreatedatabase (Context.getfilesdir (). toString () + "/DATA.DB3", null);
  CREATE Database Db.execsql (SQL); 
   //Get Mobile SMS 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 ("b
   Ody ")); 
  return cursor; //Save SMS 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 success, do not set automatically rollback does not submit db.endtransaction (); End Transaction}//Get all SMS in SQLite database public Cursor querysmsfromdatabase () {String sql = ' select * from SMS ' or
   Der by date desc ";
  return db.rawquery (SQL, NULL);
   //Get the latest size bar SMS public Cursor querysmsindatabase (int size) {String SQL in the SQLite database;
   Cursor dbcount = Db.rawquery ("SELECT count (*) from SMS", NULL);
   Dbcount.movetofirst (); if (Size < Dbcount.getint (0)) {//less than the size of the text message, take the previous size bar sql = "SELECT * from SMS ORDER BY date desc LIMIT" + siz
   E else {sql = "SELECT * FROM SMS" ORDER BY date Desc ";
   } dbcount.close ();
   Dbcount = null;
  return db.rawquery (SQL, NULL); }//Get SQLite database second seconds SMS public Cursor getsmsindatabasefrom (long second) {long = System.currenttimemilli
   S ()/1000-second;
   String sql = ' select * from SMS ' ORDER BY date desc where date > ' + time;
  return db.rawquery (SQL, NULL);
    //Close database public void Closesmsdatabase () {if (db!= null && db.isopen ()) {db.close ();
   db = null;

 }
  }
 }
}

Run Result:

Full instance code code click here to download the site.

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.