Android call history and contact Query

Source: Internet
Author: User

From http://blog.csdn.net/davayunhuijia/article/details/7778240

There are three types of call records:

Call: CallLog. CILS. INCOMING_TYPE (Constant Value: 1)

Allocated: CallLog. CILS. OUTGOING_TYPE (Constant Value: 2)

Not connected: CallLog. CILS. MISSED_TYPE (Constant Value: 3)

View the declaration in the source code:

<ProviderAndroid: Name = "CallLogProvider"Android: Authorities = "call_log"Android: Syncable = "false"Android: Multiprocess = "false"Android: ReadPermission ="Android. Permission. READ_CONTACTS"Android: WritePermission ="Android. Permission. WRITE_CONTACTS ">

</Provider>

Permission to be declared

<Uses-permissionAndroid: Name ="Android. Permission. READ_CONTACTS "/>

<Uses-permissionAndroid: Name ="Android. Permission. WRITE_CONTACTS "/>

SystemCall HistoryIs the Uri CallLog. CILS. CONTENT_URI shared externally through ContentProvider:

It is equivalent to Uri. parse ("content: // call_log/CILS ");

Query all records

ContentResolver resolver = getContentResolver (); resolver. query (CallLog. CILS. CONTENT_URI, null, null, new String [] {"15101689022"}, null );

Query all records of a contact by phone number)

Resolver. query (CallLog. CILS. CONTENT_URI, null, "number =? ", New String [] {" 15101689022 "}, null );

Query all unanswered phone records of a contact (by phone number) resolver. query (CallLog. CILS. CONTENT_URI, String [] {"15101689022"}, null );

Delete A contact's most recent call ContentResolver resolver = getContentResolver ();

/* The content provider knowledge is involved here. In fact, operations are directly performed here.
Android
The database is very painful */

Cursor cursor = resolver. query (CallLog. CILS. CONTENT_URI, new String [] {"_ id"}, "number =? And (type = 1 or type = 3) ", new String [] {" 15101689022 "}," _ id desc limit 1 "); null," number =? And type = 3 ", new if (cursor. moveToFirst () {int id = cursor. getInt (0); resolver. delete (CallLog. CILS. CONTENT_URI,
"_ Id =? ", New String [] {id + ""});}

The following describes how the code is used.

Let's talk about Phone. CONTENT_URI. When getting a contact, you need to go to the url to find the data. It actually points to "content: // com. android. contacts/data/phones ". This url corresponds to the contacts table, raw_contacts table, and data table. Therefore, our data is obtained from these three tables.

The second query parameter is emphasized here.

Private static final String [] PHONES_PROJECTION = new String [] {

Phone. DISPLAY_NAME, Phone. NUMBER, Photo. PHOTO_ID, Phone. CONTACT_ID };

It means to only search for the four data shown in the Table: phone number, Avatar ID, and contact ID. If you need other data, modify the data here.

Get Mobile Phone Address Book Contact Information

/** Get the mobile phone Address Book Contact Information **/

Private void getphonecontacts (){

Contentresolver resolver = mcontext. getcontentresolver ();

// Obtain the mobile phone contact

Cursor phonecursor = resolver. Query (phone. content_uri, phones_projection, null, null );

If (phonecursor! = NULL ){

While (phonecursor. movetonext ()){

// Obtain the mobile phone number

String phonenumber = phonecursor. getstring (phones_number_index );

// When the mobile phone number is empty or empty, the current loop is skipped.

If (textutils. isempty (phonenumber ))

Continue;

// Obtain the contact name

String contactName = phoneCursor. getString (PHONES_DISPLAY_NAME_INDEX );

// Obtain the contact ID

Long contactid = phoneCursor. getLong (PHONES_CONTACT_ID_INDEX );

// Obtain the Contact Profile ID.

Long photoid = phoneCursor. getLong (PHONES_PHOTO_ID_INDEX );

// Get the Contact Profile Bitamp

Bitmap contactPhoto = null;

// If the photoid is greater than 0, it indicates that the contact has an Avatar. If no Avatar is set for this person, a default one is provided.

If (photoid> 0 ){

Uri uri = ContentUris. withAppendedId (ContactsContract. Contacts. CONTENT_URI, contactid );

InputStream input = ContactsContract. Contacts. openContactPhotoInputStream (resolver, uri );

ContactPhoto = BitmapFactory. decodeStream (input );

} Else {

ContactPhoto = BitmapFactory. decodeResource (getResources (), R. drawable. contact_photo );

}

MContactsName. add (contactName );

MContactsNumber. add (phoneNumber );

MContactsPhonto. add (contactPhoto );

}

PhoneCursor. close ();

}

}

Get mobile phone SIM card contact information

The SIM card and the mobile phone can be obtained in a similar way, but the url is a little different. Note that the SIM card does not have a contact profile.

/** Get the contact person information for the SIM card on your mobile phone **/

Private void getSIMContacts (){

ContentResolver resolver = mContext. getContentResolver ();

// Obtain the Sims Card Contact

Uri uri = Uri. parse ("content: // icc/adn ");

Cursor phoneCursor = resolver. query (uri, PHONES_PROJECTION, null, null,

Null );

If (phonecursor! = NULL ){

While (phonecursor. movetonext ()){

// Obtain the mobile phone number

String phonenumber = phonecursor. getstring (phones_number_index );

// When the mobile phone number is empty or empty, the current loop is skipped.

If (textutils. isempty (phonenumber ))

Continue;

// Obtain the contact name

String contactname = phonecursor

. GetString (PHONES_DISPLAY_NAME_INDEX );

// No contact profile in SIM card

MContactsName. add (contactName );

MContactsNumber. add (phoneNumber );

}

PhoneCursor. close ();

}

}

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.