Android SMS (2) -- read the SMS and save it to SQLite

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 = 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 = 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 = 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; </P> <p> Public smshander (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 = 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.