Android study notes 22 -------------- get a contact by text message number

Source: Internet
Author: User

Before use


After use



Public void readmediamessage ()
{
// SMS Database Query
ContentResolver cr = getContentResolver ();
Cursor cursor = cr. query (Uri. parse ("content: // sms/inbox"), null, null,
Null, null );
 
/***************************/
String msg = "";
While (cursor. moveToNext ())
{
Int phoneColumn = cursor. getColumnIndex ("address ");
Int smsColumn = cursor. getColumnIndex ("body ");
 
Msg + = cursor. getString (phoneColumn) + ":"
+ Cursor. getString (smsColumn) + "\ n ";

String contact = getContactNameFromPhoneNum (this. getApplicationContext (), cursor. getString (phoneColumn ));

GetData (contact, cursor. getString (smsColumn ));
// GetData (cursor. getString (phoneColumn), cursor. getString (smsColumn ));
}
}
Public void readmediamessage ()
{
// SMS Database Query
ContentResolver cr = getContentResolver ();
Cursor cursor = cr. query (Uri. parse ("content: // sms/inbox"), null, null,
Null, null );

/***************************/
String msg = "";
While (cursor. moveToNext ())
{
Int phoneColumn = cursor. getColumnIndex ("address ");
Int smsColumn = cursor. getColumnIndex ("body ");

Msg + = cursor. getString (phoneColumn) + ":"
+ Cursor. getString (smsColumn) + "\ n ";

String contact = getContactNameFromPhoneNum (this. getApplicationContext (), cursor. getString (phoneColumn ));

GetData (contact, cursor. getString (smsColumn ));
// GetData (cursor. getString (phoneColumn), cursor. getString (smsColumn ));
}
}
[Java]
Public String getContactNameFromPhoneNum (Context context, String phoneNum)
{
String contactName = "";
ContentResolver cr = context. getContentResolver ();
Cursor pCur = cr. query (
ContactsContract. CommonDataKinds. Phone. CONTENT_URI, null,
ContactsContract. CommonDataKinds. Phone. NUMBER + "=? ",
New String [] {phoneNum}, null );
If (pCur. moveToFirst ())
{
ContactName = pCur
. GetString (pCur
. GetColumnIndex (ContactsContract. CommonDataKinds. Phone. DISPLAY_NAME ));
PCur. close ();
}
Return contactName;
}

From running snails

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.