I am trying to implement a BroadcastReceiver to detect incoming phone calls and start a service for call recording in my Android application using Kotlin. However, I am encountering the following error in Android Studio:
package com.example.voicenote.receivers
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.telephony.TelephonyManager
class PhoneCallReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val state = intent.getStringExtra(TelephonyManager.EXTRA_STATE)
if (TelephonyManager.EXTRA_STATE_RINGING == state) {
val serviceIntent = Intent(context, CallRecordingService::class.java)
context.startService(serviceIntent)
} else if (TelephonyManager.EXTRA_STATE_OFFHOOK == state) {
val serviceIntent = Intent(context, CallRecordingService::class.java)
context.startService(serviceIntent)
} else if (TelephonyManager.EXTRA_STATE_IDLE == state) {
val serviceIntent = Intent(context, CallRecordingService::class.java)
context.stopService(serviceIntent)
}
}
}
I’ve also added the necessary permissions (READ_PHONE_STATE, RECORD_AUDIO, etc.) in my Android manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.voicenote">
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:label="voicenote"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<receiver android:name=".receivers.PhoneCallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
</manifest>
I’m getting the following error in the PhoneCallReceiver class:
'onReceive' overrides nothing
app/build.gradle
plugins {
id "com.android.application"
id "kotlin-android"
// The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
id "dev.flutter.flutter-gradle-plugin"
}
android {
namespace = "com.example.voicenote"
compileSdk = flutter.compileSdkVersion
ndkVersion = flutter.ndkVersion
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId = "org.eierc.voicenote"
// You can update the following values to match your application needs.
// For more information, see: https://flutter.dev/to/review-gradle-config.
minSdk = flutter.minSdkVersion
targetSdk = flutter.targetSdkVersion
versionCode = flutter.versionCode
versionName = flutter.versionName
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig = signingConfigs.debug
}
}
}
flutter {
source = "../.."
}
Can anyone suggest how I can fix this issue? What am I missing?
6