I am making an app in which I want to get the call logs of all incoming, outgoing and missed calls. How can I do that?
All the answers here are using managedQuery
which is now deprecated. It should be replaced with getContext().getContentResolver().query()
method instead, as mentioned here and demonstrated here.
Here is a short sample code, based on those examples:
String[] projection = new String[] {
CallLog.Calls.CACHED_NAME,
CallLog.Calls.NUMBER,
CallLog.Calls.TYPE,
CallLog.Calls.DATE
};
// String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cursor = mContext.getContentResolver().query(CallLog.Calls.CONTENT_URI, projection, null, null, null);
while (cursor.moveToNext()) {
String name = cursor.getString(0);
String number = cursor.getString(1);
String type = cursor.getString(2); // https://developer.android.com/reference/android/provider/CallLog.Calls.html#TYPE
String time = cursor.getString(3); // epoch time - https://developer.android.com/reference/java/text/DateFormat.html#parse(java.lang.String
}
cursor.close();