安卓联系人

发布于 2025-01-01 23:06:45 字数 3002 浏览 2 评论 0原文

我可以从 Android 的联系人列表中获取所有联系人(电话号码和电子邮件) 但获取所有这些需要很长时间。 为了加快速度,我已将它们存储在我的应用程序中一次。但现在我无法获取更新的联系人。它们需要如何与我的应用程序同步?

当我尝试在列表中显示它们时出现问题,因为电话号码和电子邮件的用户数据库超过 2000 个。我目前正在使用基本适配器实现来实现此目的。 有人可以帮我处理大型联系人数据库吗?

像 GroupMe n Viber 这样的应用程序可以非常快速地显示所有用户联系人,有人可以告诉我简短的解释来实现它。

String KEY_NAME = "Name";
    String KEY_NO   = "No";

    String selection = ContactsContract.CommonDataKinds.Phone.IN_VISIBLE_GROUP + " = 1";
    String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    String data="";
    String name="";
    ContactEntry contactObj;
    String id;
    String index="";


    final String[] projection = new String[]{ContactsContract.Contacts._ID , ContactsContract.Contacts.DISPLAY_NAME , ContactsContract.Contacts.HAS_PHONE_NUMBER};

    final String[] email_projection = new String[] {ContactsContract.CommonDataKinds.Email.DATA , ContactsContract.CommonDataKinds.Email.TYPE};

    final String[] phone_projection = new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE};

    ContentResolver cr = context.getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI , projection , selection , null , sortOrder);

    if(cur.getCount()>0){

        while(cur.moveToNext()){

             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) {

                // get the phone number
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI , phone_projection , 
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);


                while (pCur.moveToNext()){

                         data = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                        if(!temp.contains(data) && !data.equals(null)){
                            //Adding PhoneNumbers in List   
                        }
                } 
                    pCur.close();
            }

           Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, email_projection,
                                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",  new String[]{id}, null); 


           while (emailCur.moveToNext()){ 

                data = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                if(!temp.contains(data) && !data.equals(null)){ 
                                  //Adding Email in List
                }
            } 
            emailCur.close();
        }
    }

我已经使用了上面的代码 我如何在 CurosrAdaper 中传递所有电子邮件和电话号码的光标。 请帮助我,我还没有找到任何解决方案。

I'm able to fetch all the contacts from Contact list in android(phone numbers and emails)
but fetching all of them takes long time.
To speed up this I have stored them once in my application. But now i can't get the updated contacts. How does they need to be synced with my application?

Problem arises when I try to display them in a list because a user database of phone numbers and emails exceeds the 2000. I'm currently using base adapter implementation for this.
Can somebody help me out for large contact database?

Application like GroupMe n Viber show all user contacts very fast can somebody tell me brief explanation to achieve it.

String KEY_NAME = "Name";
    String KEY_NO   = "No";

    String selection = ContactsContract.CommonDataKinds.Phone.IN_VISIBLE_GROUP + " = 1";
    String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    String data="";
    String name="";
    ContactEntry contactObj;
    String id;
    String index="";


    final String[] projection = new String[]{ContactsContract.Contacts._ID , ContactsContract.Contacts.DISPLAY_NAME , ContactsContract.Contacts.HAS_PHONE_NUMBER};

    final String[] email_projection = new String[] {ContactsContract.CommonDataKinds.Email.DATA , ContactsContract.CommonDataKinds.Email.TYPE};

    final String[] phone_projection = new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE};

    ContentResolver cr = context.getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI , projection , selection , null , sortOrder);

    if(cur.getCount()>0){

        while(cur.moveToNext()){

             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) {

                // get the phone number
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI , phone_projection , 
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);


                while (pCur.moveToNext()){

                         data = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                        if(!temp.contains(data) && !data.equals(null)){
                            //Adding PhoneNumbers in List   
                        }
                } 
                    pCur.close();
            }

           Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, email_projection,
                                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",  new String[]{id}, null); 


           while (emailCur.moveToNext()){ 

                data = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                if(!temp.contains(data) && !data.equals(null)){ 
                                  //Adding Email in List
                }
            } 
            emailCur.close();
        }
    }

I have used the above code
How can i pass the Curser in CurosrAdaper for all Emails and Phonenumbers.
please help me i havn't found any solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

近箐 2025-01-08 23:06:45

Android提供了同步适配器来解决这个问题。可以参考这个例子来实现。
http://developer.android.com/resources/samples/SampleSyncAdapter/index.html

Android provides a sync adapter to solve this problem. you may refer to this example for the implementation.
http://developer.android.com/resources/samples/SampleSyncAdapter/index.html

不爱素颜 2025-01-08 23:06:45

我有同样的问题,我使用了ResourceCursorAdapter,它从android中获取联系人的速度与Contact API一样快......

i had same issue , i have used ResourceCursorAdapter which fetches contacts as fast as Contact API from android....

你又不是我 2025-01-08 23:06:45

我认为您需要有一个服务(或)异步任务,例如定期将联系人从手机更新到本地应用程序。否则无法同步。

I think you need to have a service (or) Asynch task something like that which periodically updates contacts from phone to your local app. Otherwise you can't synch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文