Get the implementation code for all the text messages in the Android phone _android

Source: Internet
Author: User
Tags dateformat

Java core code:

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 ', ' type '}; 
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 typecolumn = 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 (); }

Note:
1, this function is used to get all the messages on the phone, including the Inbox, Outbox, Drafts, and so on.
2, this function can run in the service subclass because the correlation function of the activity class is not used.
3, access to the text messages include: Send and receive SMS names, mobile phone numbers, text messages, text messages sent to receive the time, the type of text messages.
SMS Main structure:
_id: SMS serial number, such as
thread_id: The serial number of the conversation, such as 100, with the same cell phone number of the message, its serial number is the same
address: The sender's location, that is, mobile phone number, such as + 8613811810000
Person: sender, if the sender is a specific name in the Address book, and the Stranger is null
Date: Date, long, such as 1256539465022, you can set the date display format
Protocol: Protocol 0sms_rpoto SMS, 1mms_proto MMS
Read: Read 0 unread, 1 read
Status: SMS Status-1 receive, 0complete,64pending,128failed
Type: SMS 1 is received, 2 is issued
Body: SMS Specific Content
Service_center: SMS Service center number, such as +8613800755500
4, in order to get text messages, You need to add a permission usage description in the Androidmanifest.xml file, as follows:
<uses-permissionandroid:name= "Android.permission.READ_SMS"/>
5, this function tests the pass on the true machine.

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.