Android application with phone book synchronization?

前端 未结 2 871
心在旅途
心在旅途 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: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 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;
    }
    

提交回复
热议问题