0

我正在创建一个 android 测试应用程序,其中我有一个按钮。单击按钮时,我想将电话簿记录与我的本地数据库同步。如果电话簿中的记录不在 db 表中,则插入它,否则保持原样。那么我该怎么做呢?

4

2 回答 2

1

要从您的电话簿中获取联系人列表,您需要在AndroidManifest.XML(即android.permission.READ_CONTACTS)中写入权限。您可以使用以下方法收集联系人列表。

          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();                 
        }
     }      
   }
}

让我知道在获取联系人列表时是否有任何问题。

于 2012-07-25T06:10:04.340 回答
-1

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;
}
于 2018-05-08T05:26:04.263 回答