Read all phone numbers through phonelookup

Source: Internet
Author: User

When reading the address book of the Android system, the contacts are generally read first and then their numbers are read in a nested loop. If the number of contacts in the address book is small, the speed is acceptable, but the number of people in the address book is 1 to 2, I am afraid it will be slow. If the hardware is almost worse, the experience will be worse. Available

Contactscontract. commondatakinds. Phone. content_uri (corresponding to the contacts2.db data view metadata) view to avoid nested reading, but phonelookup. content_filter_uri cannot be used directly. Here are some tips.

Body

1. General Usage of phonelookup. content_filter_uri

Uri URI
= URI. withappendedpath (phonelookup. content_filter_uri, Uri. encode (phonenumber ));
Resolver. Query (Uri, newstring [] {phonelookup. display_name,... copy the code

For the API, see here. If you use phonelookup. content_filter_uri as follows, the illegalargumentexception error is reported.

Getcontentresolver (). Query (phonelookup. content_filter_uri ,...

Ii. Tips and usage

Cursor C
= Getcontentresolver (). Query (URI. withappendedpath (
Phonelookup. content_filter_uri, "*"), new
String [] {
Phonelookup. _ id,
Phonelookup. Number,
Phonelookup. display_name,
Phonelookup. type, phonelookup. Label}, null, null, sortorder); copy the code

This "*" means that you can obtain the names of all numbers, related contacts, and other related fields, which is much easier than searching for their numbers through contacts.

 

Reprinted from:

Blog: http://www.cnblogs.com

Farmer's uncle: http://over140.cnblogs.com

Note:

I. Database cursor
For the returned object cursor, which is encapsulated by the androidsqlite database, it is prone to errors.
1. cursor may return null
If the cursor object returned by the query is null, any method to execute the cursor must have a null pointer. This android code 40% has not noticed.
2. cursor returns 0
We can only judge whether it is null or not. If the cursor object is 0, we will execute movetofirst (). This method will be embarrassing. This is a prompt for attention outside the android development grid.
3. When traversing cursor, no movetofirst () is executed, so that the cursor position is uncertain.
4. manually close cursor after use. Do not forget the cursor. Close () method.
5. Obtain the cursor type, such as getint and getlong. Since the Boolean and long types in SQLite are all SQLite integers, remember to convert them during reading.



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.