Hello Friends!
Today I am going to share very important code for get phone call history in Android. Don't forget to add
permission-
<android:name="android.permission.READ_CALL_LOG"/>
Today I am going to share very important code for get phone call history in Android. Don't forget to add
permission-
<android:name="android.permission.READ_CALL_LOG"/>
MainActivity.java
package com.manish.callhistory; import java.util.Date; import com.manish.callhistory.R; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.provider.CallLog; import android.widget.TextView; public class MainActivity extends Activity { TextView textView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textview_call); getCallDetails(); } 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 Log :"); 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(); textView.setText(sb); } }
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/textview_call" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> </ScrollView> </RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.manish.callhistory" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <android:name="android.permission.READ_CALL_LOG"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.manish.callhistory.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Good effort man.Every time you are posting interesting topics.Carry on...
ReplyDeleteThanks Dear!
DeleteGreat Little Tutorial !........Thank you
ReplyDeleteThanks and your welcome Yamini!
DeleteHello manish, I always read your blog it is very helpful to me and others who want learn android. you always reply all off your visitors questions i like it.really you have good knowledge in your domain, you are my role model, i want be like you.i want talk with you can i have your phone number please? if you don't mind.
ReplyDeletethanks,
payal
Thanks! Thanks! Thanks ! Thanks! Thanks!
DeleteBut phone number why? If you need any please post your query here are make me email at my email-id manishupacc@gmail.com
:)
Actually i am working on face deduct application and i am facing some trouble. can you please help me? i want discuss my problem with you.
DeletePlease help me--------
Okay you can ping me at manishupacc@gmail.com
DeleteThanks!
Nice work!
Deletehello..actually i want to save complete log in arraylist so i could view contact pic associated with the number...
ReplyDeleteSo you can save it into ArrayList and display it in listview. you should create custom adapter and arraylist like. And please mind it in below version of devices you can't get Contact Image.
Deletehello..nice tutorial.. but i have error at managedQuery(CallLog.Calls.CONTENT_URI, null,
ReplyDeletenull, null, null);
what error? can you share please?
Deletecross out managedQuery
DeleteNo worry its only because of depriciate method. Just search for another option on google or leave it as it is. It will work with out issue.
DeleteThis comment has been removed by the author.
DeleteThis comment has been removed by the author.
Deleteyour welcome..
Deletehii managedQuery(CallLog.Calls.CONTENT_URI, null,
ReplyDeletenull, null, null); is not working in listfragment
what error you are getting?
Deletehii sir..am following your blog.. thank u so much..and how to get specific contact call history..
ReplyDeletewhere you are getting all contact call history, just create a condition like-
Deletewhile (managedCursor.moveToNext()) {
String phNumber = managedCursor.getString(number);
if(phNumber.equles"08888888888"){
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(); textView.setText(sb); }
}
}
I have not check it may be syntax error in code but logic is only this. so please check and modify.
Thanks!
This comment has been removed by the author.
DeleteThank you sir..
ReplyDeleteyour welcome!
Deletenice Work Man keep continue :)
ReplyDeleteThanks Irfan!
DeleteIs it also work for android version 4.2??
ReplyDeletei am getting error while running on emulator "unfortunately callhistory is stopped"
ReplyDelete& im getting question mark @ the line
Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, null, null, null);
plz help me with this !! ASAP
Your emulator have phone contact? If yes sure it should work..
Deletethere is no error but i m getting only a blank page when i m running it on my android phone
ReplyDeleteif i want to send the details on a server what else should i have to do it in that
ReplyDeleteHi Archna, If everything Okay and you have call history sure it should work. It is working code and for send call history on server you should use Async class. Please check this demo-
Deletehttp://www.androidhub4you.com/2013/03/main-thread-issue-in-android-async.html
thank you sir, but i m unable to get the output. i installed the app on my android phone it asked for permission of reading the contacts and it got installed successfully. Still when i m opening it i m only getting a blank page.
Deletehello sir, still the problem is not solved i wrote the code again but i m getting an error "unfortunately, callhistory has stopped. " both on phone as well as emulator. please help to sort it out.
ReplyDeleteis read contacts permission in your manifest?
Deleteandroid:name="android.permission.READ_CONTACTS"
In my phone it is working fine without any permission but you can try this one too-
Deleteandroid:name="android.permission.READ_CALL_LOG"
may be it will work for you..
thank you so much sir. :) .
Deleteits working fine with this permission android:name="android.permission.READ_CALL_LOG".
Thank God and thanks to Shariat, see below comment...
Deletewell on some device it is working without any permission :) but i don't know why ..
sir can you please help me with two more codes???
Deleteif i have any demo code or any link sure i will provide.
Deletewe have to get all the details of the msg sent and received from an android mobile, and moreover if we get the details of the activity done by the person on whatsapp and facebook
Delete1) yes you can get all send/receive messages list, if you need code please share your email-id.
Delete2)getting facebook and whatsapp activity some tricky, i have never try it. you should use their APIs.
thanks you so much :) my email-id is archnalal1992@gmail.com
DeleteDownload from here-
Deletehttp://www.androidhub4you.com/2014/02/android-sms-history-get-messages-record.html
https://github.com/manishsri01/SMSHistoryDemo
This comment has been removed by the author.
ReplyDeleteFollowing permissions are required
ReplyDelete1. READ_CALL_LOG
2. WRITE_CALL_LOG
Thanks dear but here we are only reading contacts so we need only this permission-
Deleteandroid:name="android.permission.READ_CALL_LOG"
Well in my phone it is working without Call_Log permission like we don't need to give camera permission.
Nice Tutorial, helped me. Thank you.....
ReplyDeleteyour welcome!
DeleteNice tutorial and i just want to ask is it possible to use this coding for charge log instead of call log? if not can you please teach me how to do it...i searched everywhere to find the coding for charge log but still it is hopeless.
ReplyDeleteCharge log means? Battery charging status? or you want know about call charges when you make call to someone? if yes then it is not possible. It is possible only by your network service provider.
DeleteYes, it is for battery charging status, the data which is the time, date, level and status will be recorded each time the USB connected to the phone for charging. Do you have any idea on how to do it?
DeleteCheck this code-
Deleteprivate BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
battryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);
//System.out.println(battryLevel + scale + temp + voltage);
}
};
thanks Manish. I just did what you explain here, but when i run my program it warns that the project unfortunately stopped. can you help me what am i supposed to do?
ReplyDeletePlease add this permission in your manifest.xml-
Deleteandroid:name="android.permission.READ_CALL_LOG"
thank you very much Manish. Do you know how i can get the missed call duration in milliseconds?
DeleteIn my knowledge you can get the time of miss call but duration of missed call is tough. Sorry no idea.
DeleteHi Sir, i need latest ten messages how will write Please send that code
ReplyDeleteHi Sir, I need Latest ten call log number , how will write please send that code Sir ..........................
ReplyDeletehey can u post call logs example by using providers(android 4.4) and call history should be send to the email.....
ReplyDeleteand my id - sravanipractice@gmail.com
thank u........
is there any difference in 4.4?
Deletehi manish, gud tutorial ....... This code is not working in fragment activity, i am getting an error in this line..Create managed query method...
ReplyDeleteCursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, null, null, null);
I think you should use Asynck task and it will work..
Deletehi manish, I want code for how to import this call log to my sd card as an xml file and is it possible to include call cost?can you please share the code.
ReplyDeletethe code is not working in real device
ReplyDeleteplease help
Main Thread issue may be here. So try to get call history inside AsYnc task.
DeleteHello,
ReplyDeleteIts really nice and informative tutorial for us. But I am having 1 query regarding the Call logs, this code works fine with single sim devices but its not working properly in case of dual sim. So can you please kindly guide on this that how can we get logs for dual sim. If the api can't do that then how can we find work around because phones like Micromax, Moto g which are dual sim devices are having in built call logs and they are providing these information correctly so that is absolutely possible but I don't know how to go ahead with that. So it would be really great if you can help me in that.
Keyur
By default it will work for sim-1. So if you need for both you need to do some code.
DeleteHow can i create my own missed call list.plz help me as soon as.thnks in advance.
ReplyDeleteOwn means? This is all call record list, you can put/use it any where.
Deletethis is mistake TextView textView=null;
ReplyDeletesolution
TextView textView
Dear sir,
ReplyDeleteI am following you and your blogs are really extraordinary and so helpful to us. Sir, can you please provide the demo application/code for the same? This will be very helpful to me.
Thank you.
Regards.
Please copy paste form above as it is not much big code..
Deletesir,
ReplyDeleteyour blog really he;ping me a lot thank you,i am trying to develop an application for birthday remainder with the existing birthday data with data.
can you please suggest me some code or something.
Hi I am getting this error:
ReplyDeletecannot find symbol
symbol : method managedQuery(android.net.Uri,,,,)
Do you think its a problem related with some import?
Thanks!
I Only get Last Missed Call on Text-View so what can i do.????
ReplyDeleteI want only Outgoing Call details. That as to detect after each call.
ReplyDeleteIt should be automatic, no need of user interaction.
It is possible to fetch the detail after each call.?
Hi,Dear Bro ur tutor is realy helpful..i need some related thing,
ReplyDeletei need offnet and onnet call summary ,plz suggest me some code
Thanks
While run this code i got the following error.so how can i solve this error.please help me...
ReplyDeletejava.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calllog/com.example.calllog.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.CallLogProvider from ProcessRecord{414366b0 3471:com.example.calllog/10108} (pid=3471, uid=10108) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
Had you added permission in your manifest?
Deleteand if needed also add-
android.permission.READ_CONTACTS
android.permission.WRITE_CONTACTS
this permission needed-
Deleteandroid:name="android.permission.READ_CALL_LOG"
Hello
ReplyDeleteI want only last call (Incoming,outgoing ,missed..etc) details ,help me ASAP
This comment has been removed by the author.
ReplyDeletehello sir
ReplyDeleteI used your code like this
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);
but in case of missed call it give duration more then 0 in some devices only.
Thanks
Thank you for the code.
ReplyDeleteCan you please send a code to read call log after every call automatic an save automatic.
Thank you in advance.
Actually sir i am reading whole call log and show like you are showing but the problem is that if call type is missed call in call log that time duration should be 0 but it give be more than 0 what should i do?
ReplyDeletesince managedquery is deprecated can you please provide the upated version of this example.?
ReplyDeleteHow to get call log for particular sim in Dual Sim device?
ReplyDeleteI think above code provide us call history from our phone, no matter from which SIM you get that. I am not sure how can you do this, please Google and let me know if you find anything.
DeleteThanks!
Hey manish how can we get the name of the user from which i have got the call
DeleteIs it work if I clear log
ReplyDeleteI don't think so, because its get your phone call history and if there is no history how can you get the record from phone database.
Deletecould you please post android automatic call recorder project
ReplyDeleteHow can i get the name i am getting everything but how can i get name
ReplyDeletehow get missedcall(or sms) per cantact ?
ReplyDeleteYou will get all data from URI and then use if condition before switch condition and if it match to a given number then only write the log.
Deletei used this int owner = managedCursor.getColumnIndex(CallLog.Calls.PHONE_ACCOUNT_COMPONENT_NAME); for getting the phone account component name used to place the call.
ReplyDeletethen, I add it in the string builder along with other fields.
The app is crashing every time
Same for PHONE_ACCOUNT_ID
Deletewhat is the error? have you given permission into manifest?
DeleteHello Manish
ReplyDeleteI have implement this codes works fine but when i see my contact log from my phone it displays more than 100 contact log history and your code give me only 24 record history so how can i get all history completely
Can't say anything without debug the app. But please check your 100 contacts have phone number. And scrollview is able to scroll till 100 contacts, may be it is scrollview issue.
DeleteDo one thing put your record into Listview instead of StringBuilder and see if it work.
Thanks B-)
ReplyDeletehow to make text files of it
ReplyDeleteWhen you get all log just write them into sd-card.
Deletesir..what about this
ReplyDeletetake a input from the user and save it and then
use in any class in android
Time show in millisecond.Help
ReplyDeleteDo something like this-
DeleteString str_date="13-09-2011";
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date date = (Date)formatter.parse(str_date);
System.out.println("Today is " +date.getTime());
it is not working... It shows an error.Please help me!!!!!
ReplyDeleteError log?
DeleteHi, thanks for your tutorial. In my application, I need to list call logs for a particular contact. I tried your method (compare phone number) but it is not very optimized and contact number and call log number are not always with same format. Any idea to do better ?
ReplyDeleteI think you can use String compare like if number.contains(95999999) someting like that.
DeleteI Want to get Contact Name . from your code i can get only the contact number, not contact name .
ReplyDeleteFrom call history you can't get the contact name. For this you can do one thing compare the number from contact database and return the name.
Deleteis it possible to get call cost
ReplyDeleteDo you think logically it is possible? Well answer is no. Without help of service provider you can't get the call cost.
DeleteThank you for your quick response.
DeleteHello Manish
ReplyDeleteI have a Android app project .
Kindly contact me.
Hello Manish
ReplyDeleteI have a Android app project .
Kindly contact me.
Drop me a test email at manishupacc@gmail.com with your contact number.
DeleteHello Manish
ReplyDeletei can get total Datausage by TrafficStats class. but i don't know how to get 2G, 3G, 4G, Wifi data usage separately...
Hello Sir... Is it possible to fetch the contact name like we get in phone call log rather than using CACHED_NAME.
ReplyDeleteHello Sir thank you for this code but plz could u help me ..I want to this code should retrieve the name of contact number as well how could i use a code to solve this problem thank you
ReplyDeletehai manish
ReplyDeletei want only last week calls insted of total log how can i do it?