Main Structure of SMS:
- _ Id => short message number, for example, 100
- Thread_id => the dialog number, for example, 100
- Address => sender address, mobile phone number. For example, + 8613811810000
- Person => sender. A number is returned, which is the serial number in the contact list. The stranger is null.
- Date => long type. For example, 1256539465022
- Protocol => Protocol 0 sms_rpoto, 1 mms_proto
- Read => whether to read 0 unread, 1 read
- Status => Status-1 received, 0 complete, 64 pending, 128 failed
- Type => type 1 is received, and 2 is sent
- Body => Short Message Content
- 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"/>