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