Android-SMS Query

Source: Internet
Author: User
Main Structure of SMS:

  1. _ Id => short message number, for example, 100
  2. Thread_id => the dialog number, for example, 100
  3. Address => sender address, mobile phone number. For example, + 8613811810000
  4. Person => sender. A number is returned, which is the serial number in the contact list. The stranger is null.
  5. Date => long type. For example, 1256539465022
  6. Protocol => Protocol 0 sms_rpoto, 1 mms_proto
  7. Read => whether to read 0 unread, 1 read
  8. Status => Status-1 received, 0 complete, 64 pending, 128 failed
  9. Type => type 1 is received, and 2 is sent
  10. Body => Short Message Content
  11. Service_center => Number of the SMS service center. For example, + 8613800755500

Text message query is implemented through contentprovider, such as querying Inbox: managedquery (URI. parse ("content: // SMS/inbox ");
Query the sender managedquery (URI. parse ("content: // SMS/Send ");

Java code

Public final static string
Sms_uri_all = "content: // SMS/"; // 0
Public final static string sms_uri_inbox = "content: // SMS/inbox"; // 1
Public final static string sms_uri_send = "content: // SMS/sent"; // 2
Public final static string sms_uri_draft = "content: // SMS/Draft"; // 3
Public final static string sms_uri_outbox = "content: // SMS/outbox"; // 4
Public final static string sms_uri_failed = "content: // SMS/failed"; // 5
Public final static string sms_uri_queued = "content: // SMS/queued"; // 6

Example: Package COM. shao. SMS; </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. util. log; <br/> Import android. widget. textview; </P> <p> public class readsmsactivity extends activity {<br/>/** called when the activity is first created. */<br/> private stati C Final string log_tag = "SMS query"; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> super. oncreate (savedinstancestate); <br/> textview TV = (textview) findviewbyid (R. id. text); </P> <p> TV. settext (""); <br/> TV. settext (getsmsandsendback ()); <br/>}< br/>/** <br/> * read SMS <br/> * @ return <br/> */<B R/> Public String getsmsandsendback () <br/>{< br/> string [] projection = new string [] {<br/> "_ id ", <br/> "Address", <br/> "person", <br/> "body", <br/> "type" <br/> }; <br/> stringbuilder STR = new stringbuilder (); <br/> try {<br/> cursor mycursor = managedquery (URI. parse ("content: // SMS"), <br/> projection, <br/> null, null, "date DESC"); <br/> Str. append (processresults (mycursor); <br/>}< br/> Catch (sqliteexception ex) <br/>{< br/> log. D (log_tag, Ex. getmessage (); <br/>}< br/> return Str. tostring (); <br/>}< br/>/** <br/> * process the SMS result <br/> */<br/> private stringbuilder processresults (cursor cur) {<br/> // todo auto-generated method stub <br/> stringbuilder sb = new stringbuilder (); <br/> If (cur. movetofirst () {</P> <p> string name; <br/> string phonenumber; <br/> string SMS; <br/> in T type; </P> <p> int namecolumn = cur. getcolumnindex ("person"); <br/> int phonecolumn = cur. getcolumnindex ("Address"); <br/> int smscolumn = cur. getcolumnindex ("body"); <br/> int typecolum = cur. getcolumnindex ("type"); </P> <p> do {<br/> // get the Field Values <br/> name = cur. getstring (namecolumn); <br/> phonenumber = cur. getstring (phonecolumn); <br/> SMS = cur. getstring (smscolumn); <br/> type = Cu R. getint (typecolum); </P> <p> system. out. println (".................................. "); <br/> system. out. println ("name" + name); <br/> system. out. println ("type" + type); <br/> system. out. println ("phonenumber" + phonenumber); <br/> system. out. println ("SMS" + SMS); <br/> system. out. println (".................................. "); <br/> Sb. append ("{"); <br/> Sb. append (name + ","); <br/> Sb. append (phonenumber + ","); <Br/> Sb. append (SMS); <br/> Sb. append ("}"); <br/> If (null = SMS) <br/> SMS = ""; <br/>}while (cur. movetonext (); <br/>}< br/> else <br/>{< br/> Sb. append ("no result! "); <Br/>}</P> <p> return sb; <br/>}< br/>}

Remember to add the permission Android. Permission. read_sms to androidmanifest. xml.

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

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.