简体   繁体   English

广播接收器在 oreo 和 pie android 中不起作用

[英]Broadcast receiver is not working in oreo and pie android

Boot broadcast receiver is not working and there is nothing in onReceive()启动广播接收器不工作,onReceive() 中没有任何内容

public class BootReceived extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
        Toast.makeText(context, intent.getAction(), Toast.LENGTH_LONG).show();

        Log.d("IfWalaBooot", intent.getAction());

        Intent intent1 = new Intent(context, MainActivity.class);
        intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent1);

        Intent tni = new Intent(context, MainService.class);
        tni.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startService(tni);

    }

} }

You need to use registerReceiver in order to work in android oreo & above. 您需要使用registerReceiver才能在android oreo及更高版本中工作。 Starting from oreo there is background restriction. 从oreo开始,存在背景限制。

You can use this link to learn about it. 您可以使用此链接进行了解。 https://www.journaldev.com/23653/android-oreo-implicit-and-explicit-broadcast-receiver https://www.journaldev.com/23653/android-oreo-implicit-and-explicit-broadcast-receiver

  1. Make sure that you have registered your receiver in Manifest . 确保您已在Manifest中注册了接收器。
  2. Implicit Broadcast Exceptions refer to this link to check if your implicit receiver is in exception list or not. 隐式广播异常引用此链接以检查您的隐式接收者是否在异常列表中。
  3. if not in exception the you can make job scheduler or register your receiver in your code itself .Here is a nice snippet you could refer on It's time to kiss goodbye to your implicit BroadcastReceivers 如果没有例外,您可以制作作业调度程序或在代码本身中注册接收器。这是一个不错的代码段,您可以参考它是时候向隐式BroadcastReceivers道别了

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

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