I’m trying my best to track the listener’s call status but having some problems when stopping the app, the app fails to track when a call comes in.
Manifest
permission
<uses-feature
android:name="android.hardware.telephony"
android:required="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission
android:name="android.permission.WRITE_SETTINGS"
tools:ignore="ProtectedPermissions" />
register broadcast receiver
<receiver
android:name=".service.CallBroadcastReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
handle when call state change
override fun onReceive(context: Context, intent: Intent) {
try {
if (intent.action == TelephonyManager.ACTION_PHONE_STATE_CHANGED) {
val state = intent.getStringExtra(TelephonyManager.EXTRA_STATE)
when (state) {
TelephonyManager.EXTRA_STATE_RINGING -> {
handleInComingCall(context)
}
TelephonyManager.EXTRA_STATE_OFFHOOK -> {
handleOutgoingCall(context)
}
}
}
} catch (e: Exception) {
e.message
}
}
thanks for any solution that help me!^^
New contributor
Lam Nguyen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.