This video shows a very simple and easy way to create a call logging Android App. In this App, it uses a very simple layout and a button to trigger call logging. However, a developer can use this concept to integrate in their much more advance App development.
In the Manifest file it declares the permission for READ_CALL_LOG. In the Java code it seeks users permission for reading call log (READ_CALL_LOG).
Further in Java code a public method is defined which is called from the onClick attribute of the button.
In the Java code it uses getContentResolver API to query for the call logs content URI. Complete Source code of this App is pasted below.
I hope you like this video. If you have any questions, suggestions or appreciations then please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Output:


Source Code:
package com.programmerworld.calllogapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CALL_LOG}, PackageManager.PERMISSION_GRANTED);
textView = findViewById(R.id.textView);
}
public void buttonCallLog(View view){
textView.setText("Call Logging Started ... ");
String stringOutput = "";
Uri uriCallLogs = Uri.parse("content://call_log/calls");
Cursor cursorCallLogs = getContentResolver().query(uriCallLogs, null,null,null);
cursorCallLogs.moveToFirst();
do {
String stringNumber = cursorCallLogs.getString(cursorCallLogs.getColumnIndex(CallLog.Calls.NUMBER));
String stringName = cursorCallLogs.getString(cursorCallLogs.getColumnIndex(CallLog.Calls.CACHED_NAME));
String stringDuration = cursorCallLogs.getString(cursorCallLogs.getColumnIndex(CallLog.Calls.DURATION));
String stringType = cursorCallLogs.getString(cursorCallLogs.getColumnIndex(CallLog.Calls.TYPE));
stringOutput = stringOutput + "Number: " + stringNumber
+ "\nName: " + stringName
+ "\nDuration: " + stringDuration
+ "\n Type: " + stringType
+ "\n\n";
}while (cursorCallLogs.moveToNext());
textView.setText(stringOutput);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.programmerworld.calllogapp">
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="132dp"
android:layout_marginTop="128dp"
android:onClick="buttonCallLog"
android:text="@string/start_logging"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.programmerworld.calllogapp"
minSdkVersion 26
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I’m trying to run the code on my physical device but calllogs does’nt showup but works fine in emulator.
It may be restricted by the OEM (Original Equipment Manufacturer) of the device or the column index of accessing the data might be different for the manufacturer (including some index not accessible).
In all cases, the best option will be to debug the code while the App is running on the phone (enable USB debugging). In case debug on physical phone is not preferred then put logging information (print in textview) after every operation in your code and check if those are correct.
Cheers
Programmer World
–
How to implement it in Webpage?
Once the data is collected in the App then it can be transferred to any of the database. Further, in the webpage the data can be displayed from these database.
Below pages may help with some of these databases:
Oracle:
https://programmerworld.co/android/how-to-connect-to-the-oracle-database-using-jdbc-driver-from-your-android-app-android-studio-example/
MS SQL:
https://programmerworld.co/android/how-to-connect-to-ms-sql-server-database-from-your-android-app-complete-steps/
Firebase:
https://programmerworld.co/android/how-to-develop-an-android-app-for-your-retail-business-b2c-using-firebase-database-source-code/
Cheers
Programmer World
–