0

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

user
  • 471
  • 2
  • 16
  • 31

1 Answers1

0

when an app is killed via "FORCE CLOSE" by the user, all the app's broadcast receiver are immediately put on pause, and the system prevents them from receiving future broadcasts until the user manually reopens that app.

This is to prevent apps working around a force-close by the user that obviously wants that app shutdown, while an incoming broadcast receiver can wake that app up again.

see here

This rule has one exception - if the app had been set to be the default Phone/SMS app, it will still be able to wake up upon getting a call / sms. I assume TrueCaller was set as a default handler to be able to workaround this limitation.

To become the default Phone handler on Android P and below - see the documentation here: https://developer.android.com/reference/android/telecom/TelecomManager#ACTION_CHANGE_DEFAULT_DIALER

On Android Q, call this method: https://developer.android.com/reference/android/app/role/RoleManager#createRequestRoleIntent(java.lang.String) with ROLE_DIALER

marmor
  • 27,641
  • 11
  • 107
  • 150