Wednesday, January 27, 2016

How to get recent contacts or all contacts in android

Raw Contact 

A raw contact represents a person's data coming from a single account type and account name. Because the Contacts Provider allows more than one online service as the source of data for a person, the Contacts Provider allows multiple raw contacts for the same person. Multiple raw contacts also allow a user to combine a person's data from more than one account from the same account type.
Most of the data for a raw contact isn't stored in the ContactsContract.RawContacts table. Instead, it's stored in one or more rows in the ContactsContract.Data table. Each data row has a column Data.RAW_CONTACT_ID that contains the RawContacts._ID value of its parent ContactsContract.RawContacts row.
 

These 2 methods will return list of Recent Contacts /All Contacts :

 public static List<ContactsModel> getRecentContacts(Context context)
    {

        List<ContactsModel> list = new ArrayList<>();

        Uri queryUri = android.provider.CallLog.Calls.CONTENT_URI;
        String[] projection = new String[]{
                ContactsContract.Contacts._ID, 
                CallLog.Calls._ID, CallLog.Calls.NUMBER, 
                         CallLog.Calls.CACHED_NAME, 
                                 CallLog.Calls.DATE};
        String sortOrder = String.format("%s limit 150 ",  
                         CallLog.Calls.DATE + " DESC");

        Cursor cursor = context.getContentResolver().query(queryUri,
 projection, null, null, sortOrder);

        while (cursor.moveToNext())
        {
            String phoneNumber = cursor.getString(cursor
                    .getColumnIndex(CallLog.Calls.NUMBER));
            String title = (cursor.getString
                                    (cursor.getColumnIndex
                                       (CallLog.Calls.CACHED_NAME)));
            ContactsModel contactsModel =
                          new ContactsModel(title == null 
                                "Unknown" : title, phoneNumber);
            if (list.contains(contactsModel))
            {
                continue;            }

            boolean alreadyadded=false; 
for (int i = 0; i < list.size(); i++)
            {
                if(list.get(i).getPhoneNumber().
                      equals(contactsModel.getPhoneNumber()))
                {
                    alreadyadded=true; 
                       break;
                }
            }


            if (!alreadyadded)
            {
                list.add(contactsModel);            }

        }
        cursor.close();
        return list;    }
//    ================================

    public static List<ContactsModel> getAllContacts(Context context)
    {

        List<ContactsModel> list = new ArrayList<>();
        ContentResolver cr = context.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)
                {
                    Cursor pCur = cr.query(
                            ContactsContract.CommonDataKinds.
                     Phone.CONTENT_URI, 
       null,   ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
                            + " = ?", 
                               new String[]{id}, null); 
 while (pCur.moveToNext())
                    {
                        String phoneNo = pCur.getString(pCur.getColumnIndex
          (ContactsContract.CommonDataKinds.Phone.NUMBER));//                                       
                    pCur.close();                }
            }
        }


        LinkedHashSet<ContactsModel> setModel
                   = new LinkedHashSet<>(list);
        return new ArrayList<>(setModel);    }
 
 
 
And Model class be like :
public class ContactsModel implements Parcelable
{
    private String Name,PhoneNumber;
    public ContactsModel(String name, String phoneNumber)
    {
        Name = name;        PhoneNumber = phoneNumber;
    public String getName()
    {
        return Name;    }

    public String getPhoneNumber()
    {
        return PhoneNumber;    }
}

Mainfest  Permissions :
<uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
 <uses-permission android:name="android.permission.READ_CALL_LOG"/> 
<uses-permission android:name="android.permission.READ_CONTACTS"/> 
<uses-permission android:name="android.permission.WRITE_CONTACTS"/> 

0 comments:

Post a Comment

Don't lose faith when you see others receive answers to their prayers

An elephant and a dog became pregnant at same time. Three months down the line the dog gave birth to six puppies. Six months later the dog...

 

G-Expo Template by Ipietoon Cute Blog Design