Android application with phone book synchronization?

前端 未结 2 868
心在旅途
心在旅途 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<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.

    0 讨论(0)
  • 2020-12-06 16:41

    public static List fetchContacts(Context context) { final String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME_PRIMARY;

        final String FILTER = DISPLAY_NAME + " NOT LIKE '%@%'";
    
        final String ORDER = String.format("%1$s COLLATE NOCASE", DISPLAY_NAME);
        final String[] PROJECTION = {
                ContactsContract.Contacts._ID,
                DISPLAY_NAME,
                ContactsContract.Contacts.HAS_PHONE_NUMBER
        };
        ArrayList<MyContact> contacts = new ArrayList<>();
        try {
            ContentResolver cr = context.getContentResolver();
            Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, PROJECTION, FILTER, null, ORDER);
            if (cursor != null && cursor.moveToFirst()) {
                do {
                    MyContact contact = new MyContact();
                    // get the contact's information
                    String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    contact.setContactId(id);
    
                    String name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME));
                    contact.setUserName(name);
                    Integer hasPhone = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                    // get the user's email address
                    String email = null;
                    Cursor ce = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Email.DATA},
                            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);
                    if (ce != null && ce.moveToFirst()) {
                        email = ce.getString(ce.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                        ce.close();
                    }
                    contact.setEmailAddress(email);
                    // get the user's phone number
                    String phone = null;
                    if (hasPhone > 0) {
                        Cursor cp = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER,
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_LAST_UPDATED_TIMESTAMP,
                                        ContactsContract.CommonDataKinds.Phone.PHOTO_URI},
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
                        if (cp != null && cp.moveToFirst()) {
                            phone = cp.getString(cp.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    
                            String timeStamp = cp.getString(cp.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_LAST_UPDATED_TIMESTAMP));
                            contact.setLastUpdateTimestamp(timeStamp);
    
                            String photoUri = cp.getString(cp.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
                            contact.setPhotoUri(photoUri);
    
                            cp.close();
                        }
                    }
                    contact.setPhone_number(phone);
                    contacts.add(contact);
                } while (cursor.moveToNext());
                // clean up cursor
                cursor.close();
                Utils.list = contacts;
                sendMessage(context);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return contacts;
    }
    
    0 讨论(0)
提交回复
热议问题