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>
You should register and unregister your broadcast in onResume() and onPause() methods. if you register in onStart() and unregister it in onStop().
Currently there is no way to check if a receiver is registered using the receiver reference. You have to unregister the receiver and catch the IllegalArgumentException that is thrown if it's not registered.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With