How do I get the latest call logs in Android Studio?
Please add this permission in your manifest file.
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
Add this function in your Activity file and call it in oncreate function.
public void getCallLogs() {
int flag=1;
title.setText(Html.fromHtml("<b>Call Logs</b>"));
deviceDetails.setText(Html.fromHtml(""));
StringBuilder callLogs = new StringBuilder();
ArrayList<String> calllogsBuffer = new ArrayList<String>();
calllogsBuffer.clear();
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);
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;
}
calllogsBuffer.add("\nPhone Number: " + phNumber + " \nCall Type: "
+ dir + " \nCall Date: " + callDayTime
+ " \nCall duration in sec : " + callDuration + "\n");
}
managedCursor.close();
}
How to get phone call log history of organisation work profile in my application (Published as Private app)
Overview :
When trying to call or send SMS to Work Contacts, one is first presented with a message:
You're using this app outside of your work profile.
One is then able to call or text the contact, however the SMS message or Call Log will only show the contact Phone Number and no name is displayed.
Similarly, no contact details are displayed when receiving a call.
Cause :
The Phone and SMS apps are designed and housed in the Personal Profile and when activated on Android for Work the Contacts app is housed in the Work Profile. Due to limitations in Android 5.1.1 and earlier these two profiles cannot communicate with each other, thus only the Phone Number is seen. This happens on any Android Device not specific to manufacturer. For more information, please see:
https://support.google.com/work/android/answer/6275589?hl=en
Resolution :
Upgrade to Android 6.0 Marshmallow
. In Android 6.0 Marshmallow
Google
had announced improvements for Enterprise Contacts. Android for Work activated devices display Work Contact information on Personal Phone and SMS apps
Note that the ability to access work contact information is only verified as available in Google's Phone and Google's Messenger applications.
App does not display the call log after allowing android.permission.READ_CALL_LOG once
Change your if block to this , you are not handling the case when permission is already given.
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.READ_CALL_LOG), 101)
}else{
displayLog()
}
I want to read call logs in the android
1. Create observer:
class CustomContentObserver extends ContentObserver {
public CustomContentObserver(Handler handler) {
super(handler);
}
@Override public boolean deliverSelfNotifications() {
return false;
}
public void logCallLog() {
long dialed;
String columns[]=new String[] {
CallLog.Calls._ID,
CallLog.Calls.NUMBER,
CallLog.Calls.DATE,
CallLog.Calls.DURATION,
CallLog.Calls.TYPE};
Cursor c;
c = getContentResolver().query(Uri.parse("content://call_log/calls"),
columns, null, null, "Calls._ID DESC"); //last record first
while (c.moveToNext()) {
dialed=c.getLong(c.getColumnIndex(CallLog.Calls.DATE));
Log.i("CallLog","type: " + c.getString(4) + "Call to number: "+number+", registered at: "+new Date(dialed).toString());
}
}
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.d("PhoneService", "StringsContentObserver.onChange( " + selfChange + ")");
logCallLog();
}
}
2. Register observer:
Uri mediaUri = android.provider.CallLog.Calls.CONTENT_URI;
Log.d("PhoneService", "The Encoded path of the media Uri is "
+ mediaUri.getEncodedPath());
CustomContentObserver custObser = new CustomContentObserver(handler);
imageContentRsr.registerContentObserver(mediaUri, false, custObser);
EDIT:
As of Jellybean (4.1), you now need the permission:
<uses-permission android:name="android.permission.READ_CALL_LOG" />
For this to work and not throw a Permission Denial exception
getting the call logs of incoming and outgoing calls in android programmatically
Please refer the following link:
Get Android phone call history/log programmatically
If you have issues with the link above, please click here.
Related Topics
Google Play on Android 4.0 Emulator
How to Join Two SQLite Tables in My Android Application
How to Display a Pdf Document into a Webview
Override Home and Back Button Is Case a Boolean Is True
Android Recyclerview VS Listview with Viewholder
Add and Remove Views in Android Dynamically
How to Create a Circular Progressbar in Android Which Rotates on It
Android Asynctask for Long Running Operations
How to Compile Android Application with System Permissions
Updating Paging 3 Alpha to Stable Cause Indexing Issue Android
How to Parse JSON String in Android
How to Show Enable Location Dialog Like Google Maps
Android, Canvas: How to Clear (Delete Contents Of) a Canvas (= Bitmaps), Living in a Surfaceview
Android Alarmmanager Not Working on Some Devices When the App Is Closed