在Android通讯录 [英] contacts in android

查看:160
本文介绍了在Android通讯录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从设备android.i所有手机联系人使用了下列code.but的问题是,它需要更多的时间来回报results.is有什么解决办法?

  ContentResolver的CR = getContentResolver();
        INT索引= 0;
        光标CUR = cr.query(ContactsContract.Contacts.CONTENT_URI,
                NULL,NULL,NULL,NULL);        如果(cur.getCount()大于0)
        {
            phoneNames =新的String [cur.getCount()];
            PHONENUMBERS =新的String [cur.getCount()];
        而(cur.moveToNext())
        {
            字符串ID = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
             名称= cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));        如果(的Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))大于0)
        {             phoneNames [指数] =名称;
            光标pCur = cr.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    空值,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +=?,
                    新的String [] {ID},NULL);
                    而(pCur.moveToNext())
                    {
                        phoneIndex ++;
                        PHONENUMBERS [指数] = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        指数++;
                    }
                    pCur.close();            }
        }


解决方案

阅读code我假设你想要的是显示名称及其各自的电话号码的联系人列表后。

如果您是专门找相关的电话号码的数据我建议你在查询
使用单一的游标android.provider.ContactsContract.PhoneLookup并获取结果。
以下是你会感兴趣的领域:
显示名称
HAS_PHONE_NUMBER

TYPE

例如

 开放的URI = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.en code(phoneNumber的));
 resolver.query(URI,新的String [] {PhoneLookup.DISPLAY_NAME,...

更多详情,请参阅

如果假设是不正确的,请发表您的要求。


  • 一些快速检查:


    1. 选择只有所需的列,而不是所有在第一个查询。

    2. 而不是使用的Integer.parseInt(cur.getString)使用
      cur.getInt()

    3. 每当有电话号码的使用打交道PhoneLookup(数
      现场给出的原始电话号码
      代替存储在值
      它可以包含搜索数据库
      - , - ),(与它附加)

    4. 避免游标内使用光标。使用的API,其中包括
      联接在它已经实现如下
      RawContactsEntity,PhoneLookup。


希望有所帮助。

I want to get all phone contacts from device in android.i have used the following code.but the problem is it takes more time to return the results.is there any solution?

ContentResolver cr = getContentResolver();
        int index=0;
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);

        if (cur.getCount() > 0) 
        {
            phoneNames=new String[cur.getCount()];
            phoneNumbers=new String[cur.getCount()];
        while (cur.moveToNext())
        {
            String id = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
             name = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));



        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
        {

             phoneNames[index]=name;
            Cursor pCur = cr.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                    new String[]{id}, null);


                    while (pCur.moveToNext()) 
                    {
                        phoneIndex++;
                        phoneNumbers[index] = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        index++;
                    } 
                    pCur.close();

            }     
        }  

解决方案

After reading the code i assume that what you want is a list of contacts with DISPLAY NAMES and their respective phone numbers.

If you are specifically looking for data related to phone numbers i suggest you query on android.provider.ContactsContract.PhoneLookup and fetch the results using a single cursor. The following are the fields that you would be interested in: DISPLAY_NAME HAS_PHONE_NUMBER NUMBER TYPE

e.g

Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
 resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME,...

Further details please refer this

Please post your requirement if the assumptions are not true.

  • Some of quick checks:

    1. Select only the required columns and not all in the first query.
    2. Instead of using Integer.parseInt(cur.getString) use cur.getInt()
    3. Use PhoneLookup whenever dealing with phone numbers ( the number field gives the raw phone number instead of the value stored in the database which can contain
      -,),( appended with it)
    4. Avoid using Cursor within a cursor. Use the API's which includes joins already implemented in it like RawContactsEntity, PhoneLookup.

Hope that helps.

这篇关于在Android通讯录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆