Android contacts (2) -- association between SMS messages and contacts

Source: Internet
Author: User
Tags dateformat

Android SMS reads text messages and can get the sender/recipient's mobile phone number (Address). Contacts contacts can filter the mobile phone number (address), so SMS can use the mobile phone number (address) associate with a contacts Contact

SMS-Contacts association code

// Display name of the contacts Contact associated with the address mobile phone number <br/> private string getpeoplenamefromperson (string address) {<br/> If (address = NULL | Address = "") {<br/> return "(no address) \ n "; <br/>}</P> <p> string strperson = "null"; <br/> string [] projection = new string [] {Phone. display_name, phone. number };</P> <p> URI uri_person = Uri. withappendedpath (contactscontract. commondatakinds. phone. content_filter_uri, address); // address mobile phone number filtering <br/> cursor = getcontentresolver (). query (uri_person, projection, null); </P> <p> If (cursor. movetofirst () {<br/> int index_peoplename = cursor. getcolumnindex (phone. display_name); <br/> string strpeoplename = cursor. getstring (index_peoplename); <br/> strperson = strpeoplename; <br/>}< br/> cursor. close (); </P> <p> return strperson; <br/>}

Example code of SMS-Contacts Association:

Package COM. homer. phone; </P> <p> Import Java. SQL. date; <br/> Import Java. text. simpledateformat; </P> <p> Import android. app. activity; <br/> Import android. database. cursor; <br/> Import android. database. SQLite. sqliteexception; <br/> Import android.net. uri; <br/> Import android. OS. bundle; <br/> Import android. provider. contactscontract; <br/> Import android. provider. contactscontract. commondatakinds. phone; <br/> im Port android. util. log; <br/> Import android. widget. scrollview; <br/> Import android. widget. textview; </P> <p> public class phoneread2 extends activity {</P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p> textview TV = new textview (this); <br/> TV. settext (getsmsinphone (); </P> <p> scrollview SV = new scrollview (this); <br/> Sv. addview (TV); </ P> <p> setcontentview (SV); <br/>}</P> <p> Public String getsmsinphone () {<br/> final string sms_uri_all = "content: // SMS/"; <br/> final string sms_uri_inbox =" content: // SMS/inbox "; <br/> final string sms_uri_send =" content: // SMS/sent "; <br/> final string sms_uri_draft =" content: // SMS/Draft "; <br/> final string sms_uri_outbox =" content: // SMS/outbox "; <br/> final string sms_uri_failed =" content: // SMS/FA Iled "; <br/> final string sms_uri_queued =" content: // SMS/queued "; </P> <p> stringbuilder smsbuilder = new stringbuilder (); </P> <p> try {<br/> URI uri = Uri. parse (sms_uri_all); <br/> string [] projection = new string [] {"_ id", "Address", "person", "body", "date ", "type" };< br/> cursor cur = getcontentresolver (). query (Uri, projection, null, null, "date DESC"); // obtain the internal SMS of the mobile phone </P> <p> If (cur. movetofirst () {<br /> Int index_address = cur. getcolumnindex ("Address"); <br/> int index_person = cur. getcolumnindex ("person"); <br/> int index_body = cur. getcolumnindex ("body"); <br/> int index_date = cur. getcolumnindex ("date"); <br/> int index_type = cur. getcolumnindex ("type"); </P> <p> do {<br/> string straddress = cur. getstring (index_address); <br/> int intperson = cur. getint (index_person); <br/> string strbody = cur. gets Tring (index_body); <br/> long longdate = cur. getlong (index_date); <br/> int inttype = cur. getint (index_type); </P> <p> simpledateformat dateformat = new simpledateformat ("yyyy-mm-dd hh: mm: SS "); <br/> date d = new date (longdate); <br/> string strdate = dateformat. format (d); </P> <p> string strtype = ""; <br/> If (inttype = 1) {<br/> strtype = "receive "; <br/>} else if (inttype = 2) {<br/> strtype = "send"; <br/>} Else {<br/> strtype = "null"; <br/>}</P> <p> string straddress2 = getpeoplenamefromperson (straddress ); </P> <p> smsbuilder. append ("["); <br/> // smsbuilder. append (straddress + ","); <br/> smsbuilder. append (straddress + ":" + straddress2 + ","); <br/> smsbuilder. append (intperson + ","); <br/> smsbuilder. append (strbody + ","); <br/> smsbuilder. append (strdate + ","); <br/> smsbuilder. append (strtype); <br /> Smsbuilder. append ("] \ n"); <br/>}while (cur. movetonext (); </P> <p> If (! Cur. isclosed () {<br/> cur. close (); <br/> cur = NULL; <br/>}< br/>} else {<br/> smsbuilder. append ("no result! "); <Br/>}// end if </P> <p> smsbuilder. append (" getsmsinphone has executed! "); </P> <p >}catch (sqliteexception ex) {<br/> log. D ("sqliteexception in getsmsinphone", Ex. getmessage (); <br/>}</P> <p> return smsbuilder. tostring (); <br/>}</P> <p> // display name of the contacts Contact associated with the address mobile phone number <br/> private string getpeoplenamefromperson (string address) {<br/> If (address = NULL | Address = "") {<br/> return "(no address) \ n "; <br/>}</P> <p> string strperson = "null"; <br/> string [] projection = new string [] {Phone. display_name, phone. number };</P> <p> URI uri_person = Uri. withappendedpath (contactscontract. commondatakinds. phone. content_filter_uri, address); // address mobile phone number filtering <br/> cursor = getcontentresolver (). query (uri_person, projection, null); </P> <p> If (cursor. movetofirst () {<br/> int index_peoplename = cursor. getcolumnindex (phone. display_name); <br/> string strpeoplename = cursor. getstring (index_peoplename); <br/> strperson = strpeoplename; <br/>}< br/> cursor. close (); </P> <p> return strperson; <br/>}</P> <p >}< br/>

Androidmanifest. xml permission

Remember to add it to androidmanifest. xml.Android. Permission. read_smsAndAndroid. Permission. read_contactsThe two permission

<Uses-Permission Android: Name ="Android. Permission. read_sms"/>
<Uses-Permission Android: Name ="Android. Permission. read_contacts"/>

Running result:

Sample Code

Reference recommendations:

Read contacts from Android

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.