NAVIGATION - SEARCH

Android - How To Read Call Logs Or Display Call Logs Data

Android stores all call activities of your phone in the CALL_LOG content provider. This content provider has all the details about the incoming, outgoing, or missed calls. We can query the CALL_LOG content provider to get these details. This can be very useful if you plan to develop some app which shows calls data analysis, etc.

To get the calls log details, we can query the CALL_LOG content provider and then iterating through the results by using a Cursor object. The following code retrieves all the call details like incoming, outgoing and missed calls and sorts them by date. You may display this data as you wish. We are simply printing this data to Logcat console with the following details about the call — ID, phone number, date of call, type of call and whether you have seen its details or not.

public class MainActivity extends Activity 
{
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		GetCallDetailLogs();
	}

	private void GetCallDetailLogs() 
	{
		String[] argProjection = null;
		String argSelection = null;
		String[] argSelectionArgs = null;
		String argSortOrder = android.provider.CallLog.Calls.DATE + " DESC";
		Cursor cursor = null;
		try
		{
			cursor = this.getContentResolver().query( Uri.parse("content://call_log/calls"), argProjection, argSelection, argSelectionArgs, argSortOrder);
			if(cursor != null)
			{
				while (cursor.moveToNext()) 
				{
					String ID = cursor.getString(cursor.getColumnIndex( android.provider.CallLog.Calls._ID));
					String PhoneNumber = cursor.getString(cursor.getColumnIndex( android.provider.CallLog.Calls.NUMBER));
					String CallDate = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.DATE));

					String CallType = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.TYPE));
					String strCallType = "Unknown";
					if(CallType.equalsIgnoreCase("1"))
						strCallType = "Incoming";
					if(CallType.equalsIgnoreCase("2"))
						strCallType = "Outgoing";
					if(CallType.equalsIgnoreCase("3"))
						strCallType = "Missed";
					
					String flagNewCall = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.NEW));
					String strflagNewCall = "Unknown";
					if(flagNewCall.equalsIgnoreCase("0"))
						strflagNewCall = "Viewed";
					if(flagNewCall.equalsIgnoreCase("1"))
						strflagNewCall = "Pending View";

					Log.d("", "ID: " + ID);
					Log.d("", "PhoneNumber: " + PhoneNumber);
					Log.d("", "CallDate: " + CallDate);
					Log.d("", "CallType: " + strCallType);
					Log.d("", "Status: " + strflagNewCall);
				}
			}
		} 
		catch (Exception ex)
		{
			Log.d("", "ERROR: " + ex.toString());
		} 
		finally
		{
			cursor.close();
		}
	}
}

In order to query the CALL_LOG content provider, you need to add the READ_CONTACTS permission to the AndroidManifest.xml file

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

Add comment