Android obtains all text messages from mobile phones.

Source: Internet
Author: User
Tags dateformat

The principle is that contentprovider obtains the field information in the system SMS database to obtain the content.

As follows:

The Code is as follows:

Package COM. internal. message; import Java. text. simpledateformat; import Java. util. arraylist; import Java. util. date; import Java. util. hashmap; import Java. util. list; import Java. util. map; import android. app. listactivity; import android. content. contentresolver; import android. database. cursor; import android. database. SQLite. sqliteexception; import android.net. uri; import android. OS. bundle; import android. provider. contactscontract; import android. provider. contactscontract. commondatakinds. phone; import android. provider. contactscontract. phonelookup; import android. util. log; import android. widget. listview; import android. widget. simpleadapter; public class quresms extends listactivity {listview smslist = NULL; // display list information arraylist <Map <string, Object> mdata = new arraylist <Map <string, object >>> (); List <string> Title = new arraylist <string> (); // SMS source list <string> text = new arraylist <string> (); // SMS content @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); smslist = getlistview (); getsmsinphone (); int lengh = title. size (); For (INT I = 0; I <lengh; I ++) {Map <string, Object> item = new hashmap <string, Object> (); item. put ("title", title. get (I); item. put ("text", text. get (I); mdata. add (item);} simpleadapter adapter = new simpleadapter (this, mdata, android. r. layout. simple_list_item_2, new string [] {"title", "text"}, new int [] {android. r. id. text1, android. r. id. text2}); setlistadapter (adapter);}/*** get the short message */private void getsmsinphone () {final string sms_uri_all = "content: // SMS /"; /* final string sms_uri_inbox = "content: // SMS/inbox"; final string sms_uri_send = "content: // SMS/sent"; final string sms_uri_draft = "content: // SMS/Draft "; */try {contentresolver Cr = getcontentresolver (); string [] projection = new string [] {" _ id "," Address ", "person", "body", "date", "type"}; Uri uri = Uri. parse (sms_uri_all); cursor cur = CR. query (Uri, projection, null, null, "date DESC"); If (cur. movetofirst () {string name; string phonenumber; string smsbody; string date; string type; // int namecolumn = cur. getcolumnindex ("person"); int phonenumbercolumn = cur. getcolumnindex ("Address"); int smsbodycolumn = cur. getcolumnindex ("body"); int datecolumn = cur. getcolumnindex ("date"); int typecolumn = cur. getcolumnindex ("type"); do {phonenumber = cur. getstring (phonenumbercolumn); // name = cur. getstring (namecolumn); The obtained contact is considered empty, so I use the following method to obtain name = getpeoplenamefromperson (phonenumber); smsbody = cur. getstring (smsbodycolumn); simpledateformat dateformat = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); Date d = new date (Long. parselong (cur. getstring (datecolumn); Date = dateformat. format (d); int typeid = cur. getint (typecolumn); If (typeid = 1) {type = "receive";} else if (typeid = 2) {type = "send ";} else {type = "draft";} title. add (Type + "" + date + '\ n' + phonenumber); text. add (name + '\ n' + smsbody); If (smsbody = NULL) smsbody = "";} while (cur. movetonext ();} cur. close (); cur = NULL;} catch (sqliteexception ex) {log. E ("sqliteexception in getsmsinphone", Ex. getmessage () ;}}/*** use the address mobile phone number to associate the display name of the contacts Contact * @ Param address * @ return */private string getpeoplenamefromperson (string address) {If (address = NULL | Address = "") {return NULL;} string strperson = "null"; string [] projection = new string [] {Phone. display_name, phone. number}; Uri uri_person = Uri. withappendedpath (contactscontract. commondatakinds. phone. content_filter_uri, address); // address mobile phone number filtering cursor = getcontentresolver (). query (uri_person, projection, null); If (cursor. movetofirst () {int index_peoplename = cursor. getcolumnindex (phone. display_name); string strpeoplename = cursor. getstring (index_peoplename); strperson = strpeoplename;} else {strperson = address;} cursor. close (); cursor = NULL; return strperson ;}}

In fact, it is easy to get the database content. I hope it will help you.

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.