Android SMS Summary

Source: Internet
Author: User
Tags dateformat
Public String 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 "; stringbuilder smsbuilder = new stringbuilder (); try {contentresolver Cr = getcontentresolver (); string [] projection = new string [] {"_ id", "Address", "person", "body", "date", "ty PE "}; 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 typeco Lumn = cur. getcolumnindex ("type"); do {name = cur. getstring (namecolumn); phonenumber = cur. getstring (phonenumbercolumn); 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 = "";} smsbuilder. append ("["); smsbuilder. append (name + ","); smsbuilder. append (phonenumber + ","); smsbuilder. append (smsbody + ","); smsbuilder. append (date + ","); smsbuilder. append (type); smsbuilder. append ("]"); If (smsbody = NULL) smsbody = "";} while (cur. movetonext ();} else {smsbuilder. append ("no result! ");} Smsbuilder. append (" getsmsinphone has executed! ");} Catch (sqliteexception ex) {log. D (" sqliteexception in getsmsinphone ", Ex. getmessage () ;}return smsbuilder. tostring ();}

Code Description:

1. This function is used to obtain all text messages on the mobile phone, including inbox, mail box, and draft box.

2. This function can run in the service subclass because the related functions of the activity class are not used.

3. The obtained text message includes: the name of the person sending and receiving the text message, the mobile phone number, the content of the text message, the time when the text message was sent and received, and the type of the text message.

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 isNull
  5. Date => dateLongType. 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

4. To get text messages, you need to add permission instructions in the androidmanifest. xml file, as shown below:

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


Save the sent text message to the sender

 private void storeMsg(String destinationAddress, String text)    {        ContentValues cv = new ContentValues();        cv.put("address", destinationAddress);        cv.put("person", "");        cv.put("protocol", "0");        cv.put("read", "1");        cv.put("status", "-1");        cv.put("body", text);        this.getContentResolver().insert(Uri.parse("content://sms/sent"), cv);        Log.e("tl3shi", "msg has stored");    

The system automatically matches the session information of the same person. Note that the above Code permission must be added for read and write, because when writing, it also reads fields such as ID in the database.

<uses-permission android:name="android.permission.WRITE_SMS"/> <uses-permission android:name="android.permission.READ_SMS"


This is a code for deleting text message sessions.

Uri mUri=Uri.parse("content://sms/conversations/" + id);         mContext.getContentResolver().delete(mUri, null, null);  

Recommended articles:

Text Message Management Code http://www.cnblogs.com/GnagWang/archive/2011/01/06/1929075.html in Android

 

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.