Android SMS read SQLite save

Source: Internet
Author: User
Tags dateformat idate

SMS messages of Android are stored in the SQLite database in the Android system, but are not accessible to other programs (Security Mechanism of Android)

Now we are reading SMS messages from our mobile phone, saving them in our own SQLite database, then reading the SQLite database to extract text messages and display

SMS SQLite access code:

Package com. homer. sms; </p> <p> import java. SQL. date; <br/> import java. text. simpleDateFormat; </p> <p> import org. loon. wsi. r; </p> <p> import android. app. activity; <br/> import android. content. context; <br/> import android. database. cursor; <br/> import android. database. sqlite. SQLiteDatabase; <br/> import android. graphics. color; <br/> import android.net. uri; <br/> import android. OS. bundle; <br/> import android. util. Log; <br/> import android. widget. tableLayout; <br/> import android. widget. tableRow; <br/> import android. widget. tableRow. layoutParams; <br/> import android. widget. textView; </p> <p>/** <br/> * read the text message and save it to SQLite data first, then read the database display * <br/> * @ author sunboy_2050 <br/> * @ since http://blog.csdn.net/sunboy_2050 <br/> * @ date 2012.03.06 <br/> */ <br/> public class smsRead4 extends Activity {</p> <p> TableLayou T tableLayout; <br/> int index = 0; </p> <p> @ Override <br/> public void onCreate (Bundle savedInstanceState) {<br/> super. onCreate (savedInstanceState); </p> <p> setContentView (R. layout. main); </p> <p> tableLayout = (TableLayout) findViewById (R. id. tableLayout); <br/> showSMS (); <br/>}</p> <p> private void showSMS () {<br/> SmsHander smsHander = new SmsHander (this); </p> <p> smsHander. createSMSDatabase (); // Number of SQLite instances created <Br/> smsHander. insertSMSToDatabase (); // read the text message and insert it into the SQLite database. <br/> Cursor cursor = smsHander. querySMSInDatabase (100); // obtain the first 100 messages (sorted by date) </p> <p> cursor. moveToPosition (-1); <br/> while (cursor. moveToNext () {<br/> String strAddress = cursor. getString (cursor. getColumnIndex ("address"); <br/> String strDate = cursor. getString (cursor. getColumnIndex ("date"); <br/> String strBody = cursor. getString (cursor. ge TColumnIndex ("body"); </p> <p> SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "); <br/> Date date = new Date (Long. parseLong (strDate); <br/> strDate = dateFormat. format (date); </p> <p> String smsTitle = strAddress + "\ t" + strDate; <br/> String smsBody = strBody + "\ n "; <br/> Log. I ("tableRow", smsTitle + smsBody); </p> <p> // title Row <br/> TableRow trTitle = new TableRow (this); <br/> TrTitle. setLayoutParams (new LayoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); </p> <p> TextView tvTitle = new TextView (this); <br/> tvTitle. setText (smsTitle); <br/> tvTitle. getPaint (). setFakeBoldText (true); // bold font <br/> tvTitle. setTextColor (Color. RED); <br/> tvTitle. setLayoutParams (new LayoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); <br/> trTitle. addView (tvTitle ); <Br/> tableLayout. addView (trTitle, new TableLayout. layoutParams (LayoutParams. FILL_PARENT, LayoutParams. WRAP_CONTENT); </p> <p> // body Row <br/> TableRow trBody = new TableRow (this); <br/> trBody. setLayoutParams (new LayoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); </p> <p> TextView tvBody = new TextView (this); <br/> tvBody. setText (smsBody); <br/> tvBody. setLayoutParams (new LayoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); <br/> trBody. addView (tvBody); <br/> tableLayout. addView (trBody, new TableLayout. layoutParams (LayoutParams. FILL_PARENT, LayoutParams. WRAP_CONTENT); <br/>}</p> <p> if (! Cursor. isClosed () {<br/> cursor. close (); <br/> cursor = null; <br/>}</p> <p> smsHander. closeSMSDatabase (); <br/> index = 0; <br/>}</p> <p> public class SmsHander {</p> <p> SQLiteDatabase db; <br/> Context context; </p> <p> public SmsHander (Context context) {<br/> this. context = context; <br/>}</p> <p> public void createSMSDatabase () {<br/> String SQL = "create table if not exists sms (" <br/> + "_ id integer prima Ry key autoincrement, "<br/> +" address varchar (255), "+" person varchar (255), "<br/> +" body varchar (1024 ), "+" date varchar (255), "<br/> +" type integer) "; <br/> db = SQLiteDatabase. openOrCreateDatabase (context. getFilesDir (). toString () + "/data. db3 ", null); // create a database <br/> db.exe cSQL (SQL ); <br/>}</p> <p> // get sms <br/> private Cursor getSMSInPhone () {<br/> Uri SMS_CONTENT = Uri. parse ("content: // sms/"); <B R/> String [] projection = new String [] {"_ id", "address", "person", "body", "date", "type "}; <br/> Cursor cursor = context. getContentResolver (). query (SMS_CONTENT, projection, null, null, "date desc"); // get sms </p> <p> while (cursor. moveToNext () {<br/> System. out. println ("-- sms --:" + cursor. getString (cursor. getColumnIndex ("body"); <br/>}</p> <p> return cursor; <br/>}</p> <p> // Save the text message sent to the SQLite database. <B R/> public void insertSMSToDatabase () {<br/> Long lastTime; <br/> Cursor dbCount = db. rawQuery ("select count (*) from sms", null); <br/> dbCount. moveToFirst (); <br/> if (dbCount. getInt (0)> 0) {<br/> Cursor dbcur = db. rawQuery ("select * from sms order by date desc limit 1", null); <br/> dbcur. moveToFirst (); <br/> lastTime = Long. parseLong (dbcur. getString (dbcur. getColumnIndex ("date"); <br/>} else {<br /> LastTime = new Long (0); <br/>}< br/> dbCount. close (); <br/> dbCount = null; </p> <p> Cursor cur = getSMSInPhone (); // get SMS (Cursor) <br/> db. beginTransaction (); // start transaction processing <br/> if (cur. moveToFirst () {<br/> String address; <br/> String person; <br/> String body; <br/> String date; <br/> int type; </p> <p> int iAddress = cur. getColumnIndex ("address"); <br/> int iPerson = cur. getColumnIndex ("person"); <br/> int iBody = Cur. getColumnIndex ("body"); <br/> int iDate = cur. getColumnIndex ("date"); <br/> int iType = cur. getColumnIndex ("type"); </p> <p> do {<br/> address = cur. getString (iAddress); <br/> person = cur. getString (iPerson); <br/> body = cur. getString (iBody); <br/> date = cur. getString (iDate); <br/> type = cur. getInt (iType); </p> <p> if (Long. parseLong (date)> lastTime) {<br/> String SQL = "insert into sms values (null, ?, ?, ?, ?, ?) "; <Br/> Object [] bindArgs = new Object [] {address, person, body, date, type}; <br/> db.exe cSQL (SQL, bindArgs ); <br/>}else {<br/> break; <br/>}< br/>}while (cur. moveToNext (); </p> <p> cur. close (); <br/> cur = null; <br/> db. setTransactionSuccessful (); // sets whether the transaction is successfully processed. If this parameter is not set, automatic rollback is performed and no commit is performed. <br/> db. endTransaction (); // end transaction processing <br/>}</p> <p> // obtain all text messages from the SQLite database <br/> public Cursor querySMSFromDatabase () {<br /> String SQL = "select * from sms order by date desc"; <br/> return db. rawQuery (SQL, null); <br/>}</p> <p> // obtain the latest size SMS in the SQLite database <br/> public Cursor querySMSInDatabase (int size) {<br/> String SQL; </p> <p> Cursor dbCount = db. rawQuery ("select count (*) from sms", null); <br/> dbCount. moveToFirst (); <br/> if (size <dbCount. getInt (0) {// if the size of the message is insufficient, the first size message is used. <br/> SQL = "select * from sms order Date desc limit "+ size; <br/>} else {<br/> SQL =" select * from sms order by date desc "; <br/>}< br/> dbCount. close (); <br/> dbCount = null; </p> <p> return db. rawQuery (SQL, null); <br/>}</p> <p> // obtain the first second message of the SQLite database <br/> public Cursor getSMSInDatabaseFrom (long second) {<br/> long time = System. currentTimeMillis ()/1000-second; <br/> String SQL = "select * from sms order by date desc where da Te> "+ time; <br/> return db. rawQuery (SQL, null); <br/>}</p> <p> // close the database <br/> public void closeSMSDatabase () {<br/> if (db! = Null & db. isOpen () {<br/> db. close (); <br/> db = null; <br/>}</p> <p >}< br/>Running result:

Sample Code

Recommendation reference:

SMS reading for Android

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.