7

I'm Working on a project which records incoming calls and outgoing calls, but with Oreo static broadcast receivers (Broadcast receivers which are registered in the manifest) are not getting triggered. if I register with context, the broadcast will not be triggered once app gets killed.

I want the broadcast receiver to work even if app is closed.

is there any possible way to achieve this Oreo? or any other alternative approach to achieve this? any help will be appreciated.

I'm registering in manifest as below code

<application ...
  ..
    <receiver
            android:name=".PhoneCallReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>
</application>
Jayanth
  • 5,954
  • 3
  • 21
  • 38
  • Could you paste your code here? – Shalu T D Apr 02 '18 at 15:51
  • Its System restricted. I do not think there is way to omit this behaviour . have a look at [Broadcast Limitations](https://developer.android.com/about/versions/oreo/background.html#broadcasts) if you already haven't. if you are using one of the listed Broadcast . – ADM Apr 02 '18 at 15:54
  • @ShaluTD I have pasted code – Jayanth Apr 02 '18 at 15:55
  • @ADM but Truecaller apps works fine in my Oreo phone. it shows call details before and after every call ends. – Jayanth Apr 02 '18 at 15:56
  • what info do you need ? – Heshan Sandeepa Apr 02 '18 at 17:19
  • currently, if the user swipes and kills the app, the broadcast receiver is not getting triggered. the broadcast receiver only works if the app is active or the app is minimized. I want to know how can I make it work even if app gets killed. – Jayanth Apr 02 '18 at 17:56
  • no , i mean what are the out-going/incoming call details you need ? is calling number sufficient ? – Heshan Sandeepa Apr 02 '18 at 18:01
  • yes, that's all i need only calling number – Jayanth Apr 02 '18 at 18:18

2 Answers2

7

There are some Broadcast Limitations in Oreo, it no longer supports to registering broadcast receivers for implicit broadcasts in app manifest. And NEW_OUTGOING_CALL is one of them, read here

You can use PHONE_STATE action for your purpose as it hasn't categorized as a implicit broadcasts yet

public class StateReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
   // will trigger at incoming/outgoing call

    try {
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        String outgoingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
    }
    catch (Exception e){
        e.printStackTrace();
    }
  }
}

In manifest,

        <receiver android:name=".StateReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
        </receiver>

Also you need to add and check READ_PHONE_STATE permission

Heshan Sandeepa
  • 3,388
  • 2
  • 35
  • 45
3

There are some Implicit Broadcast Exceptions which you can find here https://developer.android.com/guide/components/broadcast-exceptions and ACTION_NEW_OUTGOING_CALL is among them.

My similar answer here: https://stackoverflow.com/a/51354698/1399483

Kevin Worth
  • 421
  • 4
  • 15