簡體   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