w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Sync phone Contacts with Database Android application

Start from Contacts Provider

Ultimately you have to use

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

The above code retrives all phone numbers with name using the below interface in your cursor

/**
 * 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;
}

The sync logic has to written seperately. Try it and post your comments :)





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.