Broadcast receiver for nought and Oreo + devices not working

青春壹個敷衍的年華 提交于 2021-01-27 18:22:34

问题


public class ScreenReceiver extends BroadcastReceiver {

    private boolean screenOff;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))    {
            screenOff = true;
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            screenOff = false;
        }

    }


   <receiver
             android:name=".ScreenReceiver"
             android:enabled="true"
             android:exported="true">
             <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
                 <action android:name="android.intent.action.DREAMING_STARTED" />
                <action android:name="android.intent.action.DREAMING_STOPPED" />
                 <action android:name="android.intent.action.CLOSE_SYSTEM_DIALOGS" />
                 <action android:name="android.intent.action.SCREEN_ON" />
                 <action android:name="android.intent.action.SCREEN_OFF" />
                <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
                 <action android:name="android." />
             </intent-filter>
        </receiver>

Not getting any callback on naught and oreo devices,tried on marshmallow devices its working fine .but on oreo devices its not working and also for battery connected and network change receiver not working .


回答1:


You can not register broadcast receiver in manifest.xml from Oreo. You can see Android 8.0 Behavior Changes

Apps cannot use their manifests to register for most implicit broadcasts (that is, broadcasts that are not targeted specifically at the app).

Solution

Register your receiver in your related Activity instead. Like this.

public class MainActivity extends AppCompatActivity {
    BroadcastReceiver receiver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction("android.intent.action.LOCKED_BOOT_COMPLETED");
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // todo
            }
        };
        registerReceiver(receiver, filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (receiver != null)
            unregisterReceiver(receiver);
    }
}

You can add action as string same as manifest, if you don't find relevant constant string.



来源:https://stackoverflow.com/questions/52364398/broadcast-receiver-for-nought-and-oreo-devices-not-working

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