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>
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
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