Read call Logs in Android example

Read call logs in Android here are the simple steps.

1) Update Manifest file with following permissions.

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

2) Use CallLog.Calls.CONTENT_URI to read call logs & traverse the cursor.


    //Fetches the complete call log in descending order. i.e recent calls appears first.
 	Cursor c = getApplicationContext().getContentResolver().query(CallLog.Calls.CONTENT_URI, projection, null, 
              null, CallLog.Calls.DATE + " DESC");

    	if (c.getCount() > 0)
         {
    	    c.moveToFirst();
    	    do{
	        String callerID = c.getString(c.getColumnIndex(CallLog.Calls._ID));
                String callerNumber = c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));
	        long callDateandTime = c.getLong(c.getColumnIndex(CallLog.Calls.DATE));
	        long callDuration = c.getLong(c.getColumnIndex(CallLog.Calls.DURATION));
	        int callType = c.getInt(c.getColumnIndex(CallLog.Calls.TYPE));
        	if(callType == CallLog.Calls.INCOMING_TYPE)
	  	{
	           //incoming call	
	    	}
	    	else if(callType == CallLog.Calls.OUTGOING_TYPE)
	    	{
	    		//outgoing call
	    	}
	    	else if(callType == CallLog.Calls.MISSED_TYPE)
	    	{
	    		//missed call
	    	}
              }while(c.moveToNext());

    	}

Filter call logs based on projection

Add filter to get what all you need from call logs i.e ID, Number, Date on which the call is made, Type of call i.e incoming or outgoing call, duration of the call, name of the contact etc… for more¬†identifier refer Call Logs in Android from official website.

String[] projection = new String[] {
    			CallLog.Calls._ID,
    			CallLog.Calls.NUMBER,
    			CallLog.Calls.DATE,
    			CallLog.Calls.DURATION,
    			CallLog.Calls.TYPE
    			};

Cursor c = getApplicationContext().getContentResolver().query(CallLog.Calls.CONTENT_URI, projection, null, 
              null, CallLog.Calls.DATE + " DESC");

Filtering call Logs based on number, then use below code



String whereClause = CallLog.Calls.NUMBER + " = " + filterNumber;

 Cursor c =  mct.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, CallLog.Calls.NUMBER + " = ? " , 
            		new String[]{filterNumber}, CallLog.Calls.DATE + " DESC");