Android application with phone book synchronization?

前端 未结 2 867
心在旅途
心在旅途 2020-12-06 15:56

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 b

2条回答
  •  粉色の甜心
    2020-12-06 16:19

    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 nameList;
               ArrayList 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.

提交回复
热议问题