Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Registering Static Broadcast receiver in Oreo

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>
like image 776
Jayanth Avatar asked Apr 02 '18 15:04

Jayanth


People also ask

Where do I register and unregister broadcast receiver?

You should register and unregister your broadcast in onResume() and onPause() methods. if you register in onStart() and unregister it in onStop().

How do you check broadcast receiver is registered or not in Android?

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.


1 Answers

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

like image 106
Heshan Sandeepa Avatar answered Sep 28 '22 09:09

Heshan Sandeepa