简体   繁体   English

广播接收器,用于零播和Oreo +设备不起作用

[英]Broadcast receiver for nought and Oreo + devices not working

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 . 在零启动和奥利奥设备上没有得到任何回调,在棉花糖设备上尝试了它的正常工作。但是在奥利奥设备上,它没有工作,并且由于电池连接和网络更改接收器不工作。

You can not register broadcast receiver in manifest.xml from Oreo. 您无法在Oreo的manifest.xml中注册广播接收器。 You can see Android 8.0 Behavior Changes 您可以看到Android 8.0的行为更改

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. 如果找不到相关的常量字符串,则可以将动作添加为与清单相同的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM