Android development-get sms

Source: Internet
Author: User

How to Get text messages today

The first step is to apply for permissions in AndroidMainfest:

The following is the code for getting a text message:

// Set all text messages

Public void getMobileMessage (Intent m ){

Final String SMS_URI_ALL = "content: // sms/"; // all text messages

Final String SMS_URI_INBOX = "content: // sms/inbox"; // No text message is sent.

Final String SMS_URI_SEND = "content: // sms/sent"; // you have sent an sms.

Final String SMS_URI_DRAFT = "content: // sms/draft"; // draft box

Int count = 0;

StringBuilder smsBuilder = new StringBuilder ();

Try {

ContentResolver cr = m. 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 = "";


Count ++;

} While (cur. moveToNext () & count <10); // read the first 10 messages sorted by default.

} Else {

SmsBuilder. append ("no result! ");

}


SmsBuilder. append ("getSmsInPhone has executed! ");

} Catch (SQLiteException ex ){

Log. d ("SQLiteException in getSmsInPhone", ex. getMessage ());

}


If you have reprinted the results of your hard work, please indicate the source. Thank you!



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.