繁体   English   中英

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

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

启动广播接收器不工作,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);

    }

}

您需要使用registerReceiver才能在android oreo及更高版本中工作。 从oreo开始,存在背景限制。

您可以使用此链接进行了解。 https://www.journaldev.com/23653/android-oreo-implicit-and-explicit-broadcast-receiver

  1. 确保您已在Manifest中注册了接收器。
  2. 隐式广播异常引用此链接以检查您的隐式接收者是否在异常列表中。
  3. 如果没有例外,您可以制作作业调度程序或在代码本身中注册接收器。这是一个不错的代码段,您可以参考它是时候向隐式BroadcastReceivers道别了

暂无
暂无

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

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