与数据库Android应用程序同步手机联系人

时间:2014-10-11 17:03:13

标签: android sync android-contacts contact contact-form

和Iam目前正在开发一个同步用户联系人的Android应用程序 并通过在数据库中搜索来显示那些人作为用户的朋友。 这使用了与whatsapp非常相似的技术。 我对此很新。如果有人能帮助我,那将非常友好。请告诉我,我应该从哪里开始。也请给我一些参考。 任何帮助都应该是可取的。请帮忙。 事先提前: - ))))

1 个答案:

答案 0 :(得分:0)

Contacts Provider

开始

最终你必须使用

new CursorLoader(getActivity(),
                contentUri,
                ContactMobileNumbQuery.PROJECTION,
                ContactMobileNumbQuery.SELECTION,
                null,
                ContactMobileNumbQuery.SORT_ORDER);

以上代码使用光标中的以下界面

重新搜索所有带姓名的电话号码
/**
 * This interface defines constants used by mobile number retrieval queries.
 */
public interface ContactMobileNumbQuery{
    final static int QUERY_ID = 1;

    //A Content Uri for Phone table
    final static Uri CONTENT_URI = Phone.CONTENT_URI;   

    //The search or filter query Uri
    final static Uri FILTER_URI = Phone.CONTENT_FILTER_URI;

    // The selection clause for the CursorLoader query. The search criteria defined here
    // restrict results to contacts that have a phone number and display name.
    // Notice that the search on the string provided by the user is implemented by appending
    // the search string to CONTENT_FILTER_URI.
    final static String SELECTION = Phone.HAS_PHONE_NUMBER + "=1" + " AND " + Phone.DISPLAY_NAME_PRIMARY + "<>''";

    // The desired sort order for the returned Cursor - Order by DISPLAY_NAME_PRIMARY 
    //in Ascending with case insensitively
    final static String SORT_ORDER = Phone.DISPLAY_NAME_PRIMARY + " COLLATE NOCASE ASC";

    // The projection for the CursorLoader query. This is a list of columns that the Contacts
    // Provider should return in the Cursor.
    final static String[] PROJECTION = {

        // The contact's row id
        Phone._ID,

        // the Contacts table contains DISPLAY_NAME_PRIMARY, which either contains 
        // the contact's displayable name or some other useful identifier such as an 
        // email address.
        Phone.DISPLAY_NAME_PRIMARY,

        // A pointer to the contact that is guaranteed to be more permanent than _ID. Given
        // a contact's current _ID value and LOOKUP_KEY, the Contacts Provider can generate
        // a "permanent" contact URI.
        Phone.LOOKUP_KEY,

        //Phone number of the contact
        Phone.NUMBER,
    };
    // The query column numbers which map to each value in the projection
    final static int ID = 0;
    final static int DISPLAY_NAME = 1;
    final static int LOOKUP_KEY = 2;
    final static int NUMBER = 3;
}

同步逻辑必须单独写入。试试并发表您的意见:)