DEV Community

AbdelHalim Mahmoud
AbdelHalim Mahmoud

Posted on • Updated on

How to track phone calls in your app in Android

Phone call tracking is a useful feature that can be added to Android apps for various purposes. With the help of this feature, an app can keep track of different phone call events, such as incoming calls, outgoing calls, missed calls, etc. In this article, we will discuss how you can add phone call tracking functionality to your Android app using the Call Tracker library.

Call Tracker Library Features
The Call Tracker library offers the following features:

  • Handling different call events, including incoming call received, incoming call ended, incoming call answered, missed call, outgoing call ended, and outgoing call started.
  • Providing information about each call event, such as the phone number, start time, and end time.

Permissions required

To use the Call Tracker library, you need to include the following permissions in your AndroidManifest.xml file:

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_CALL_LOG" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
Enter fullscreen mode Exit fullscreen mode

Usage

Follow these steps to use the Call Tracker library in your Android app:

Step 1. Add the JitPack repository to your build file

    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
Enter fullscreen mode Exit fullscreen mode

Step 2: Add the library dependency

Add the following dependency to your app's build.gradle file:

dependencies {
    implementation 'com.github.abdomi7:call-tracker:1.0.3'
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Extend PhoneCallReceiver

Extend the PhoneCallReceiver class and implement the methods provided for each call event you want to track.

class CallTrackingReceiver : PhoneCallReceiver() {

    override fun onIncomingCallReceived(context: Context, number: String?, start: Date) {
        super.onIncomingCallReceived(context, number, start)
        Log.d("CallRecordReceiver", "onIncomingCallReceived: $number started at $start")
    }

    override fun onIncomingCallEnded(context: Context, number: String?, start: Date, end: Date) {
        super.onIncomingCallEnded(context, number, start, end)
        Log.d("CallRecordReceiver", "onIncomingCallEnded: $number started at $start and ended at $end")
    }

    override fun onIncomingCallAnswered(context: Context, number: String?, start: Date) {
        super.onIncomingCallAnswered(context, number, start)
        Log.d("CallRecordReceiver", "onIncomingCallAnswered: $number started at $start")
    }

    override fun onMissedCall(context: Context, number: String?, start: Date) {
        super.onMissedCall(context, number, start)
        Log.d("CallRecordReceiver", "onMissedCall: $number started at $start")
    }

    override fun onOutgoingCallEnded(context: Context, number: String?, start: Date, end: Date) {
        super.onOutgoingCallEnded(context, number, start, end)
        Log.d("CallRecordReceiver", "onOutgoingCallEnded: $number started at $start and ended at $end")
    }

    override fun onOutgoingCallStarted(context: Context, number: String?, start: Date) {
        super.onOutgoingCallStarted(context, number, start)
        Log.d("CallRecordReceiver", "onOutgoingCallStarted: $number started at $start")
    }

    override fun onReceive(context: Context, intent: Intent) {
        super.onReceive(context, intent)
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Create a foreground service and launch the Call Tracker. For example:

class CallsService : Service() {
    private var record: CallTracker? = null
    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        createNotificationChannels()
        record = getInstance()
        record!!.setPhoneCallReceiver(CallTrackingReceiver())
        record!!.startCallTrackingReceiver(this)
        val intent1 = Intent(applicationContext, MainActivity::class.java)
        var pendingIntent: PendingIntent? = null
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            pendingIntent = PendingIntent.getActivities(applicationContext, 0, arrayOf(intent1), PendingIntent.FLAG_IMMUTABLE)
        }
        val notification = NotificationCompat.Builder(this, CHANNEL_1_ID)
            .setContentText("Service is running")
            .setContentTitle("Service enabled")
            .setSmallIcon(R.mipmap.ic_launcher).setContentIntent(pendingIntent)
        startForeground(100, notification.build())
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onDestroy() {
        super.onDestroy()
        record!!.stopCallTrackingReceiver(this)
    }

    override fun onBind(intent: Intent): IBinder? {
        return null
    }

    private fun createNotificationChannels() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel1 = NotificationChannel(
                CHANNEL_1_ID,
                "Channel 1",
                NotificationManager.IMPORTANCE_HIGH
            )
            channel1.description = "This is Channel 1"
            val manager = getSystemService(NotificationManager::class.java)
            manager.createNotificationChannel(channel1)
        }
    }

    companion object {
        const val CHANNEL_1_ID = "channel1"
    }
}
Enter fullscreen mode Exit fullscreen mode

when you want to start the service you can simply call

val startIntent = Intent(this, CallsService::class.java)

startService(startIntent)
Enter fullscreen mode Exit fullscreen mode

Library source code: CallTracker

Top comments (0)