Android SMS reading

Source: Internet
Author: User

Android SMS read

Package COM. homer. SMS; </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. util. log; <br/> Import android. widget. scrollview; <br/> Import android. widget. tablelayout; <br/> Import android Oid. widget. textview; </P> <p>/** <br/> * read SMS <br/> * @ author sunboy_2050 <br/> * @ since http://blog.csdn.net/sunboy_2050 <br/> * @ date 2012.03.06 <br/> */<br/> public class smsread 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/failed "; <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 intpers On = cur. getint (index_person); <br/> string strbody = cur. getstring (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> smsbuilder. append ("["); <br/> smsbuilder. append (straddress + ","); <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/>}< br/>}

Androidmanifest. xml permission

 Remember to add it to androidmanifest. xml.Android. Permission. read_smsThis permission

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


Running result:

Sample Code


Uri mainly includes:

Content: // SMS/all text messages
Content: // SMS/inbox
Content: // SMS/sent
Content: // SMS/Draft draft
Content: // SMS/outbox sender
Content: // SMS/failed to send
Content: // SMS/queued list to be sent

Main Structure of SMS: 

    1. _ id => short message number, such as 100
    2. thread_id => the conversation sequence number, for example, 100
    3. address => sender address, mobile phone number. For example, + 8613811810000
    4. person => sender. A number is the serial number in the contact list returned. The stranger is null
    5. date => 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

String [] projection = new string [] {"Address", "body "};
Cursor cursor = getcontentresolver (). Query (Uri, projection, "Where .." New String [] {"", ""}, "order ..")


Android SMS storage database

It happened that a class mmssmsdatabasehelper. Java in the android source code was found. In the past, Android stored all the text message informationMmssms. DB.

This class is not available in public sdks and cannot be used directly. Therefore, I wrote a sqliteopenhelper myself, but an SQL exception occurred during the query.It seems that you cannot do whatever you want, but according to the online information, you can copy dB files to back up text message data.

Mmssmsdatabasehelper. Java path in Android source code:

Packages/providers/telephonyprovider/src/COM/Android/providers/telephony/mmssmsdatabasehelper. Java


The fields in the SMS database are as follows:

_ Id An auto-increment field, starting from 1
Thread_id Serial number. the ID of the same sender is the same.
Address Sender's mobile phone number
Person The serial number in the contact list. The stranger is null.
Date Date of delivery
Protocol Protocol, divided into: 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
All = 0;
Inbox = 1;
Sent = 2;
Draft = 3;
Outbox = 4;
Failed = 5;
Queued = 6;

Body Text message content
Service_center Number of the SMS Service Center
Subject Text Message Subject
Reply_path_present TP-reply-Path
Locked


Source code of the field type in the SMS database table:

Private void createsmstables (sqlitedatabase dB) {<br/> // n. b.: whenever the columns here are changed, the columns in <br/> // {@ ref mmssmsprovider} must be changed to match. <br/> db.exe csql ("create table sms (" + <br/> "_ id integer primary key," + <br/> "thread_id integer, "+ <br/>" address text, "+ <br/>" person integer, "+ <br/>" date integer, "+ <br/>" date_sent integer default 0, "+ <br/>" protocol integer, "+ <br/>" read integer default 0, "+ <br/>" status integer default-1, "+ // a TP-status value <br/> // or-1 if it <br/> // status hasn't <br/> // been unbound ed <br /> "type Integer, "+ <br/>" reply_path_present integer, "+ <br/>" Subject text, "+ <br/>" body text, "+ <br/>" service_center text, "+ <br/>" locked integer default 0, "+ <br/>" error_code integer default 0, "+ <br/>" seen integer default 0 "+ <br/>"); "); <br/> .... <br/>}

Packages/providers/telephonyprovider/src/COM/Android/providers/telephony/mmssmsdatabasehelper. Java


The contact is empty.

In the text message database, if you receive a strange text message before adding a stranger to the contact list, the person field in the text message database is null. if you add a contact and then send a text message, the person field in the text message database is not empty, so if you want to obtain other contact information through the field in the text message database, you can only use the address.

Reference recommendations:

Android SMS messaging

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.