THIS CONTENT DOWNLOAD SHORTLY

Objective

The main objective of this tutorial is to help you get call logs from android devices and insert call logs into devices.

 

You will get Final Output:

  • call-logs1
  • call-logs-2
  • call-logs-3
 

Step 1 Create CallLogHelper class

First we create CallLogHelper class into our project. In this class we create methods for Retrieve all the call logs and insert new entry into call log.

 

Step 2 getAllCallLogs() method

Add this method in to CallLogHelper class for getting all the Call Logs. This method returns the cursor of all the calls.

private Cursor getAllCallLogs(ContentResolver cr) {
		// reading all data in descending order according to DATE
		String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
		Uri callUri = Uri.parse("content://call_log/calls");
		Cursor cur = cr.query(callUri, null, null, null, strOrder);
		// loop through cursor
		while (cur.moveToNext()) {
			String callNumber = cur.getString(cur
					.getColumnIndex(android.provider.CallLog.Calls.NUMBER));

			String callName = cur
					.getString(cur
							.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));

			String callDate = cur.getString(cur
					.getColumnIndex(android.provider.CallLog.Calls.DATE));
			SimpleDateFormat formatter = new SimpleDateFormat(
					"dd-MMM-yyyy HH:mm");
			String dateString = formatter.format(new Date(Long
					.parseLong(callDate)));

			String callType = cur.getString(cur
					.getColumnIndex(android.provider.CallLog.Calls.TYPE));

			String isCallNew = cur.getString(cur
					.getColumnIndex(android.provider.CallLog.Calls.NEW));

			String duration = cur.getString(cur
					.getColumnIndex(android.provider.CallLog.Calls.DURATION));

			// process log data...
		}
	}

To know more about CallLog.Calls:

 

Step 3 insertPlaceholderCall() method

Add this method in to CallLogHelper class for inserting new Call Log with Name and Number.

public static void insertPlaceholderCall(ContentResolver contentResolver,
			String name, String number) {
		ContentValues values = new ContentValues();
		values.put(CallLog.Calls.NUMBER, number);
		values.put(CallLog.Calls.DATE, System.currentTimeMillis());
		values.put(CallLog.Calls.DURATION, 0);
		values.put(CallLog.Calls.TYPE, CallLog.Calls.OUTGOING_TYPE);
		values.put(CallLog.Calls.NEW, 1);
		values.put(CallLog.Calls.CACHED_NAME, name);
		values.put(CallLog.Calls.CACHED_NUMBER_TYPE, 0);
		values.put(CallLog.Calls.CACHED_NUMBER_LABEL, "");
		Log.d("Call Log", "Inserting call log placeholder for " + number);
		contentResolver.insert(CallLog.Calls.CONTENT_URI, values);
	}
 

Step 4 Calling Mehod

Call above two method into your Main method for getting and inserting the call logs. For this, Download the Example which is attached with this tutorial.

To run this example on MarshMallow, you have to check permissions at runtime.

I hope you enjoy this tutorial and it would be helpful to you. Got an Idea of Android App Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Android App Development Company in India.

An entrepreneur who has founded 2 flourishing software firms in 7 years, Tejas is keen to understand everything about gaming - from the business dynamics to awesome designs to gamer psychology. As the founder-CEO of a company that has released some very successful games, he knows a thing or two about gaming. He shares his knowledge through blogs and talks that he gets invited to.