Registering Static Broadcast receiver in Oreo

懵懂的女人 提交于 2020-01-03 16:00:08

问题


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>

回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/49614203/registering-static-broadcast-receiver-in-oreo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!