Saturday, August 20, 2011

Get Android phone call history/log programmatically

To get call history programmatically first add read conact permission in Manifest file :
<uses-permission android:name="android.permission.READ_CONTACTS" />

Create xml file. Add the below code in xml file :

<Linearlayout android:layout_height="fill_parent"
 android:layout_width="fill_parent"
android:orientation="vertical">
<Textview android:id="@+id/call"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</Textview>
</Linearlayout>

Now call the getCallDetails() method in java class :

private void getCallDetails() {

StringBuffer sb = new StringBuffer();
Cursor managedCursor = managedQuery( CallLog.Calls.CONTENT_URI,null, null,null, null);
int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER );
int type = managedCursor.getColumnIndex( CallLog.Calls.TYPE );
int date = managedCursor.getColumnIndex( CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex( CallLog.Calls.DURATION);
sb.append( "Call Details :");
while ( managedCursor.moveToNext() ) {
String phNumber = managedCursor.getString( number );
String callType = managedCursor.getString( type );
String callDate = managedCursor.getString( date );
Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = managedCursor.getString( duration );
String dir = null;
int dircode = Integer.parseInt( callType );
switch( dircode ) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;

case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;

case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
sb.append( "\nPhone Number:--- "+phNumber +" \nCall Type:--- "+dir+" \nCall Date:--- "+callDayTime+" \nCall duration in sec :--- "+callDuration );
sb.append("\n----------------------------------");
}
managedCursor.close();
call.setText(sb);
}

Using this application user can access his call history. It show call number,call type i.e. incoming/outgoing/missed call, call date-time and call duration.
Here is the output where user can see all details :







73 comments:

Sara said...

Thank you very much for such an nice tutorial. It was of great help to me. But it gets stopped unexpectedly. I am knew to android And don't know how to fix it.
i copied your code and did exactly what you said. Imported all the required packages. I don't see any mistake :(
I am using android 2.2 SDK 8.

AndroidDev said...

Sara have you initialized xml and text view in your activity class before calling getCallDetails() method. Add below source in onCreate() method :
setContentView(R.layout.main);
call = (TextView)findViewById(R.id.call);
getCallDetails();

Sara said...

Thanks for your prompt reply. yeah i have initialized the xml and textview :(

Sara said...
This comment has been removed by the author.
Sara said...

I have figured it out! and i am quite ashamed of the mistake i made. I missed one line in xml :(
I am really sorry for bothering you. It is an excellent tutorial and it works fine. :) thank u

Anonymous said...

still iam getting response as unexpected close

iwyz80 said...

hello, I really found this tutorial efficient. I would want to know if I wish to sort the call logs to only outgoing international and local calls what do i need done? or what is the code i need?

Anonymous said...

Iam trying to get my call log for all my calls for both my phones

Unknown said...

It worked well ,thanks a lot

Saqi1989 said...

Hey dude nice tutorial but i am getting error in the last line
call.setText(sb);

call cannot be resolved

i m not much familiar with android.
thnxs

Anonymous said...

error in using managedQuery()

The method managedQuery(Uri, null, null, null, null) is undefined for the type CallRecord

Anonymous said...

error in managedQuery()

india call unlimited said...

I have been visiting various blogs for India call unlimited. I have found your blog to be quite useful. Keep updating your blog with valuable information... Regards

viram said...

send me complete code of Get Android phone call history/log programmatically" at virampurohit@gmail.com

Naresh said...

Very useful tutorial, Thank you very much.

Journey Beyond Limits said...

all the force close errors are because we are just copying the code.
there is an error in XML code Provide

he has written "Textview" instead of "TextView" i mean v should be capital

Unknown said...
This comment has been removed by the author.
Unknown said...

Thank you very much for such an nice tutorial.
But managedQuery is Deprecated.
is there another solution?

Anonymous said...

Hi its really a nice tutorial i have visited many sites but not find an exact example.I have an error will you please help me i cannot able to resolve the managedQuery its giving me an error CallLog.Call also not resolved have any solution please help ....!

Anonymous said...

The above will not work. We need 2 permissions for this. Add the following permissions in the manifest file:

Anonymous said...

i have pasted the code correctly n i added the menifest file and call = (TextView)findViewById(R.id.call);
getCallDetails(); in oncreate() method bt i found error in last line call.setText(sb);
how i resolve this?

Anonymous said...

call.setText(sb.toString());

isha said...

Hello Sir
can you please give me the code for retrieving the call summary(total no of incoming calls,outgoing calls,how many are local calls,std calls etc)same for sms summary how many sms are sent local,std etc.Sir i'm new to android and i have to submit my app please help me sir as soon as possible.
Thank You.

Unknown said...

Thank you very much for the cod sir.... Can you plz give me the code for accessing sms details like, sender, receiver, time, date, content etc.

AndroidDev said...

Below is the link of my posts where you can get information about how to read both incoming and outgoing SMS content programmatically.

http://android2011dev.blogspot.in/2013/10/incoming-sms-details.html

http://android2011dev.blogspot.in/2013/10/outgoing-sms-details.html

Ismael said...

In the manifest file change to:
android:targetSdkVersion="10"
then the code will run
i dont know exactly why, i think its compatibility, someone can explain it?? because if targetSdkVersion="18" for example, the app crashes

Unknown said...

Great tutorial..Thnxs alot..:)

Unknown said...

hi, when i write this code in eclipse cross out managedCursor
do you know why?

Anonymous said...

it run well in emulator but not in mobile

Archna said...

hello, i m getting an error of unfortunately, calllog has stopped both on mobile as well as emulator.
please help to sort it out.

Archna said...

hello, i m getting an error unfortunately, callog stopped both on emulator as well as phone.please help to sort it out

Unknown said...
This comment has been removed by the author.
Unknown said...

hi,
thank you for thos mus information. .... you are showing call time also.. but for me i didn't get time ... why please help me..


Thank you

Unknown said...

Thank you very much for the cod sir.... Can you plz give me the code for accessing backup of call log details like, sender, receiver, time, date, etc.

Unknown said...

very useful post. send me complete project of Get Android phone call history/log programmatically" to kchands93@gmail.com

Hema said...

hi, any one help me is it possible to get call cost also?if possible,How to get the call cost.

Unknown said...

sir it showing only 4,5 call details how we can get all call logs details

Anonymous said...

hi...Im trying to get the call cost details,Im getting the cost per minute details from the user,but still error occured. if anyone plz help me...by using this how to know call cost.

Anonymous said...

can we forward this call log history to another phone as a message

malik said...

hi i want to store all these logs in sqlite database i m new to android can any body helps me plzzz

Anonymous said...

how do i convert time in 12 hr format

Anonymous said...

Hi, thanks for this great tutorial, but my problem is that i only get the missed calls. can you please help me.

Shaunak Das said...

Change the order in the last lines of code if you don't want the system to crash after sometime.

The correct order will be
call.setText(sb); managedCursor.close();

Unknown said...

Thanks for sharing the information.
regards, Calling Card

Anonymous said...

hi, i know its a stupid question but still i want t know can i get all the conversation of call .

Thanks.

Jayaprakash Balasundaram said...

use this permission this code i get it thank you very match very use fully tutorial

Anonymous said...

use this permission

Anonymous said...

How to get call logs for particular SIM if the phone has DUAL SIM

Anonymous said...

Thanks for the post. I'm using Android Studio 1.3.1 and a Nexus 5 API 22 emulator and receive a permission issue.

Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.CallLogProvider from ProcessRecord{39b0a8ec 2019:test.com.testapp/u0a57} (pid=2019, uid=10057) requires android.permission.READ_CALL_LOG or android.permission.WRITE_CALL_LOG

I believe my manifest sets the correct permissions. Any ideas?





<application

Thanks in advance.

Unknown said...

I changed some things and it worked for me.
1st: The is misspelled, it is written "Linearlayout".
2nd: The is misspelled, it is written "Textview".
3rd: For the java.lang.SecurityException: Permission Denial you should add
.
Also a good tip: instead of you should use so that
you can scroll through all the calls.

Unknown said...

The tags from my previous post were banned so i republish:
1st: The "LinearLayout" is misspelled, it is written "Linearlayout".
2nd: The "TextView" is misspelled, it is written "Textview".
3rd: For the java.lang.SecurityException: Permission Denial you should add
'uses-permission android:name="android.permission.READ_CALL_LOG"'
Also a good tip: instead of "LinearLayout" you should use "ScrollView" so that
you can scroll through all the calls.

Anonymous said...

sir how to get current date call log history plz tell me

Unknown said...

I am not able to resolve Calls
Can anyone please help me?

Unknown said...

hi how to track last received or dialled or missed cal im getting call history but how to get last call alone

Unknown said...

how to retrieve only todays incomming calls call summary in android

Jayaprakash Balasundaram said...

Hi jyo243 filter your calls details array current date = your calls details array(date)

Anonymous said...

get only incoming call
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration);
sb.append("\n----------------------------------");
miss_cal.setText(sb);
break;

Anonymous said...

How can I get the Contact Name too??

Unknown said...

Hello sir, i want to send the call log details to an email. I request you sir to provide the complete java code for this program.

nivekalara said...

gracias for this article

DroidBlogger said...

Searching for this one.
Some times i too posts on my blog https://www.droidblogger.com

Unknown said...

managedQuery is depreciated for api 23. I am not able to resolve it. Any opinion?

sameeksha said...

guys plz answer how to make it work

Unknown said...

unexpected close error please tell me . there is an error in methoud calling. please help

Anonymous said...

Is it possible to get carrier name also in incoming call detail

Unknown said...


Nice information.. Thanks for sharing..
Digital marketing services in pune

Unknown said...

Great stuff...I have come across very many sites, but none that are so accurate and so regularly updated. Thanks for your efforts and keep up the good work new android phones Please keep share

Abhishek said...

Hello Dear,

The blog was absolutely fantastic! Lot of great information which can be helpful in some or the other way. Keep updating the blog, looking forward for more contents.

You can also get Track & Monitor Employee Calls History | Call Log App - Callmonitor.app

Call History App - Find the best call monitor app for track & monitor employees inbound and outbound calls history, get valuable insight to boost your company with us! Download now app.

Visit Now - https://callmonitor.app/

Anonymous said...

how to replace Textview to Listview. I make Listview but get an error. please solve this error
Call.setText(sb.toString()+""); error in setText.

Unknown said...

Uri allCalls = Uri.parse("content://call_log/calls");
Cursor c = managedQuery(allCalls, null, null, null, null);

String num= c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));// for number
String name= c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME));// for name
String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION));// for duration
int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));// for call type, Incoming or out going.

Shine Sreedhar said...

call.setText(sb); showing error..

Anu R said...

Accelmove Dynamics has an extraordinary and fast-growing technical team of engineers and R&D members who seamlessly combine advanced analytical thinking , design thinking and bring in Problem solving functionalities under one roof . We work on identifying challenges and implementing the best solutions in this real world, wherever robots could fit into.

https://accelmove.com/

Anonymous said...

NIL