prof_jack
prof_jack

Reputation: 2073

how to retrieve recent call list and favourites list in android?

I need to call recent call list and favourite call list on the click of respective buttons and need the data in my own list layout.I am new to android and having lot of trouble with this.can anyone please help me..thanks in advance..

Upvotes: 7

Views: 10924

Answers (3)

Annie
Annie

Reputation: 15

You can use the following Java code to get the recent calls list:

Cursor phones = getActivity().getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null,null);

while (phones.moveToNext()) {
    String phoneNumber = phones.getString(phones.getColumnIndex(CallLog.Calls.NUMBER));

    if (phoneNumber.length() > 6) {
        String name = phones.getString(phones.getColumnIndex(CallLog.Calls.CACHED_NAME));

        numbers.add(phoneNumber);

    }
} 

Upvotes: 0

Oded Breiner
Oded Breiner

Reputation: 29739

With some extra, useful code:

getFavoriteContacts:

Map getFavoriteContacts(){
Map contactMap = new HashMap();

Uri queryUri = ContactsContract.Contacts.CONTENT_URI;

String[] projection = new String[] {
        ContactsContract.Contacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME,
        ContactsContract.Contacts.STARRED};

String selection =ContactsContract.Contacts.STARRED + "='1'";

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


while (cursor.moveToNext()) {
    String contactID = cursor.getString(cursor
            .getColumnIndex(ContactsContract.Contacts._ID));

    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactID));
    intent.setData(uri);
    String intentUriString = intent.toUri(0);

    String title = (cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));

    contactMap.put(title,intentUriString);

}
cursor.close();
return contactMap;
}

getRecentContacts:

Map getRecentContacts(){
Map contactMap = new HashMap();

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 500 ", CallLog.Calls.DATE + " DESC");


Cursor cursor = 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)));

    if(phoneNumber==null||title==null)continue;

    String uri = "tel:" + phoneNumber ;
    Intent intent = new Intent(Intent.ACTION_CALL);
    intent.setData(Uri.parse(uri));
    String intentUriString = intent.toUri(0);

    contactMap.put(title,intentUriString);

}
cursor.close();
return contactMap;
}

Upvotes: 8

Hiral Vadodaria
Hiral Vadodaria

Reputation: 19250

For getting recent calls list,you can use CallLog in android. Here is a good tutorial.This is also helpful.

You can use it for all outgoing calls like this :

Cursor cursor = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI,null, android.provider.CallLog.Calls.TYPE+"="+android.provider.CallLog.Calls.OUTGOING_TYPE, null,null);

For all types of calls,use it like:

Cursor cursor = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI,null, null, null,null);

Upvotes: 5

Related Questions