I am working on a application and my requirement is to store all incoming and outgoing call details like number,duration,time
I am using broadcast receiver for this along with run time permissions READ_PHONE_STATE,READ_CALL_LOG
With the current code app is working fine when app is in foreground as well as background BUT when I kills the app,it is not working,it is not detecting incoming/outgoing calls.
Below is my code of manifest file
<receiver
android:name=".utils.CallReceiver"
android:enabled="true"
android:exported="true">
<intent-filter >
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
Broadcast receiver
override fun onReceive(context: Context?, intent: Intent) {
//We listen to two intents. The new outgoing call only tells us of an outgoing call. We use it to get the number.
if (intent.action == "android.intent.action.NEW_OUTGOING_CALL") {
savedNumber = intent.extras!!.getString("android.intent.extra.PHONE_NUMBER")
} else {
val stateStr =
intent.extras!!.getString(TelephonyManager.EXTRA_STATE)
val number =
intent.extras!!.getString(TelephonyManager.EXTRA_INCOMING_NUMBER)
var state = 0
if (stateStr == TelephonyManager.EXTRA_STATE_IDLE) {
state = TelephonyManager.CALL_STATE_IDLE
} else if (stateStr == TelephonyManager.EXTRA_STATE_OFFHOOK) {
state = TelephonyManager.CALL_STATE_OFFHOOK
} else if (stateStr == TelephonyManager.EXTRA_STATE_RINGING) {
state = TelephonyManager.CALL_STATE_RINGING
}
if (number != null && !number.isEmpty() && !number.equals("null")) {
onCallStateChanged(context, state, number);
Log.d("TEST :","NUMBER =>"+number);
return;
}
}
I need solution which can detect incoming call when app is killed like true caller app and want to start receiver on Android 7,8,9 when call happens