Manipulate Android contacts, call logs, short messages, URIs, and specific query statements, field annotations. (depending on your needs)

Source: Internet
Author: User

Remember read contacts need to configure permissions: <uses-permission android:name= "Android.permission.READ_CONTACTS"/>
<uses-permission android:name= "Android.permission.WRITE_CONTACTS"/>

Read the URI of the contact:

/**
* Query statements for all contacts
* Cotacts2 in contact database
* Several data sheets related to the contact person
* Contacts
* Data
* Mimetypes
*/
Cursor contactscusor = Context.getcontentresolver (). Query (
ContactsContract.Contacts.CONTENT_URI,
New string[] {contacts._id, contacts.photo_id,
Contacts.lookup_key}, NULL, NULL, NULL);

cursor cursor = context.getcontentresolver (). Query (
ContactsContract.Data.CONTENT_URI,
New string[] {data.mimetype, data.data1, DATA.DATA15},
data.raw_contact_id + "=" + contacts_id, NULL, NULL);
Data.data1: This field is the information of all the contact person;

Data.mimetype: This field is based on the ID of the mimetypes in this table is corresponding;
Returns a string type of data that can be matched with the following data.
Determine what the specific return (DATA.DATA1) is:

1) "VND.ANDROID.CURSOR.ITEM/PHONE_V2": Mobile phone number
2) "VND.ANDROID.CURSOR.ITEM/EMAIL_V2": Email
3) "Vnd.android.cursor.item/name": Name
4) "VND.ANDROID.CURSOR.ITEM/POSTAL-ADDRESS_V2": Address
5) "Vnd.android.cursor.item/organization": Company

DATA.DATA15: This field is the head of the query;

Realization idea: According to the contacts._id found in the previous table to the data table query, the returned DATA.DATA1 is all the contact information,
According to Data.mimetype data to go with mimetypes this table to match, to determine the specific return of the information is (name, phone ....) )

Remember to read call logs to configure permissions:

<uses-permission android:name= "Android.permission.READ_CALL_LOG"/>

<uses-permission android:name= "Android.permission.WRITE_CALL_LOG"/>


Read the URI of the call log:
/**
* Query statements for all call logs
*/
String [] projection = new string[]{calllog.calls._id,//_id of Call log
CallLog.Calls.CACHED_NAME,//name of the call record;
CallLog.Calls.NUMBER,//Phone number
CallLog.Calls.DATE,//Time of call
CallLog.Calls.TYPE}; Type of call 1 Inbound Call 2 Outgoing call 3 missed call

cursor cursor = context.getcontentresolver (). Query (CallLog.Calls.CONTENT_URI,
Projection
NULL, NULL, calllog.calls.date+ "desc");


Remember to configure permissions to read text messages:

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

Read the URI of the SMS:

/**
* All the text messages
*/
public static final String Sms_uri_all = "content://sms/";
/**
* Inbox SMS
*/
public static final String Sms_uri_inbox = "Content://sms/inbox";
/**
* Send Box SMS
*/
public static final String sms_uri_send = "Content://sms/sent";
/**
* Draft box SMS
*/
public static final String Sms_uri_draft = "Content://sms/draft";


/**
* The URI of the session
*/
public static final String Thread_uri = "Content://mms-sms/conversations"
/**
* URI for all SMS messages
*/
public static final String Sms_uri = "Content://sms"



Read the list of sessions get the last short message there (according to your needs):

Field Name: Body, this field is text message content
Field Name: Person, the value of this field is: null (the sender of the last text message in the conversation is me or a stranger value null, if it is someone in the address book, the value is the ID of the contact in the Contacts table)
Field Name: Type, the value of this field is: 2 (Type 1 receive SMS 2 SMS)
Field Name: Date, the value of this field is: 1462352825441
Field Name: _id, the value of this field is: 5 (the ID of the last text message in the SMS data table in the session)
Field Name: Read, the value of this field is: 1 (1 read 0 unread)
Field Name: thread_id, the value of this field is: 1
Field Name: Address, the value of this field is: 1 366-136-1366 (regardless of who sent the last text message, the phone that the field stores is the other person's phone in that session)


The name and avatar ID in the threads table ContentProvider still not available, need to use the phone number to phone_lookup table in reverse query.

Uri:PhoneLookup.CONTENT_FILTER_URI
Avatar Id:PhoneLookup.PHOTO_ID
Name: Phonelookup.display_name

Read all SMS messages for a session:

String [] projection = new string[] {"_id",
"Body", "date", "type"};
Contentresolver resolver = Context.getcontentresolver ();

cursor cursor = resolver.query (URI, projection, "thread_id =" +thread_id, NULL, "date ASC");


Field name: Body,: This field is SMS content
Field name: Type, which is the SMS type (type 1 SMS 2 text message)
Field Name: Date,: This field is a text message timestamp
Field name: _id,: This field is an ID in the SMS table
Field name: thread_id,: This field is the ID of the session

=============================================================================================================== =====================

Reverse the Avatar ID using the phone number:

Calls table in the PHOTO_ID column data There is a small problem, when the caller calls in, if not answered, the other person's profile picture ID value is
is not recorded in the calls table. No avatar appears when the call record is displayed in Calllogfragment. Therefore, it is necessary to use
The phone number goes to other tables to query the user's avatar ID.

The simplest way to query your avatar ID with a phone number is to use the Phone_lookup

ContentProvider to make a query. When querying, it is important to note that

The URI format supported by the ContentProvider is:
Uri.withappendedpath (ContactsContract.PhoneLookup.CONTENT_FILTER_URI, telephone number);

The complete code example that uses the phone number to query the Avatar ID is:
protected static int Getphotoidbynumber (Context context,string number) {
int photoid = 0;
Query with the contentprovider of the Phone_lookup data sheet
Contentresolver CR = Context.getcontentresolver ();
Uri uri = Uri.withappendedpath (ContactsContract.PhoneLookup.CONTENT_FILTER_URI, number);
Cursor C = cr.query (URI, new string[]{phonelookup.photo_id}, NULL, NULL, NULL);
If the phone number provided does have an avatar,
if (C.movetonext ()) {
PhotoID = c.getint (0);
}
C.close ();
return photoid;
}

=============================================================================================================== =====================

Manipulate Android contacts, call logs, short messages, URIs, and specific query statements, field annotations. (depending on your needs)

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.