Android应用程序与电话簿同步? [英] Android application with phone book synchronization?

查看:82
本文介绍了Android应用程序与电话簿同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造一款使用Android操作系统的测试应用程序,其中我有一个按钮。在按一下按钮,我想电话簿记录同步电话簿我的本地database.If记录不在数据库表,然后将其插入,否则离开它,因为它是。那么,如何能做到这一点?

I am creating one android test application , in which i have one button. On button click ,i want to synchronize phonebook records with my local database.If record in phone book is not in db table then insert it , otherwise leave it as it is. so how can i do this?

推荐答案

有关获取联系人列表从电话簿中,你需要在的Andr​​oidManifest.xml 写权限(即 android.permission.READ_CONTACTS )。而且,你可以用下面的方法收集的联系人列表。

For getting contact list from your phone book you need write permission in AndroidManifest.XML (i.e. android.permission.READ_CONTACTS) .And you can collect contact list using following method.

          ShowContact()
         {
           ArrayList<String> nameList;
           ArrayList<String> phoneNoList;

           ContentResolver cr = getContentResolver();
           Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);
           if (cur.getCount() > 0) {
          while (cur.moveToNext()) {
            String id = cur.getString(
                    cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(
                    cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
               if(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                   //Query phone here.  Covered next

                 Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                 new String[]{id}, null);
                  while (pCur.moveToNext()) {
                // Do something with phones
                    String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                    nameList.add(name); // Here you can list of contact.
                    phoneNoList.add(phoneNo); // And here you can get list of phone number.You have to query separately for getting phone_no,email,name etc 
   // Here you have to iterate this(i.e. nameList) with your list in the database.And your rest of logic.

                } 
                pCur.close();                 
        }
     }      
   }
}

,让我知道如果有在得到联系人列表中的任何问题。

And Let me know if are having any issue in getting contact list.

这篇关于Android应用程序与电话簿同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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