Skip to content Skip to sidebar Skip to footer

Android - How To Get A Contact From Call Log?

I am trying to get contacts from call log. I can get the contact numbers from main contacts using this code : public void getContacts(View view) { Intent intentContact =

Solution 1:

I got this going using my own version. i used a dialog and handed it the cursor to the call log. Here is the function:

publicvoidgetCallLog() {

    String[] callLogFields = { android.provider.CallLog.Calls._ID,
            android.provider.CallLog.Calls.NUMBER,
            android.provider.CallLog.Calls.CACHED_NAME /* im not using the name but you can*/};
    StringviaOrder= android.provider.CallLog.Calls.DATE + " DESC";
    StringWHERE= android.provider.CallLog.Calls.NUMBER + " >0"; /*filter out private/unknown numbers */finalCursorcallLog_cursor= getActivity().getContentResolver().query(
            android.provider.CallLog.Calls.CONTENT_URI, callLogFields,
            WHERE, null, viaOrder);

    AlertDialog.BuildermyversionOfCallLog=newAlertDialog.Builder(
            getActivity());

    android.content.DialogInterface.OnClickListenerlistener=newDialogInterface.OnClickListener() {
        publicvoidonClick(DialogInterface dialogInterface, int item) {
            callLog_cursor.moveToPosition(item);

            Log.v("number", callLog_cursor.getString(callLog_cursor
                    .getColumnIndex(android.provider.CallLog.Calls.NUMBER)));

            callLog_cursor.close();

        }
    };
    myversionOfCallLog.setCursor(callLog_cursor, listener,
            android.provider.CallLog.Calls.NUMBER);
    myversionOfCallLog.setTitle("Choose from Call Log");
    myversionOfCallLog.create().show();
}

Solution 2:

You can use ContactsContract.Contacts.CONTENT_STREQUENT_URI which will give you both Frequently called and Starred contacts.

Solution 3:

From API 21 is possible to use this: https://developer.android.com/reference/kotlin/android/provider/CallLog.Calls#CACHED_LOOKUP_URI

CACHED_LOOKUP_URI added in API level 21 static val CACHED_LOOKUP_URI: String The cached URI to look up the contact associated with the phone number, if it exists.

This value is typically filled in by the dialer app for the caching purpose, so it's not guaranteed to be present, and may not be current if the contact information associated with this number has changed.

Post a Comment for "Android - How To Get A Contact From Call Log?"