Solve the problem that static broadcast receivers cannot receive implicit broadcasts

In Android 8.0 and above (Build.VERSION.SDK_INT>=26), if:
1) BroadcastReceiver is statically registered in AndroidManifest.xml;
2) Send implicit broadcast.
At this time, the system will report the following error:
BroadcastQueue: Background execution not allowed: receiving Intent...
There are three solutions:

  1. Dynamically register BroadcastReceiver in code;
  2. Send an explicit broadcast, that is, specify the package name and class name;
  3. Add flag Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND to Intent.
    insert image description here
    However, since FLAG_RECEIVER_INCLUDE_BACKGROUND is annotated with **@hide , the application layer code cannot find this variable (the code cannot be successfully compiled).
    insert image description here
    So it can only be hardcoded as intent.addFlags( 0x01000000 ).
    insert image description here
    It is found that the code is still marked red in Android Studio. Although it can be successfully compiled, the line of code is not actually compiled and packaged into the apk.
    insert image description here
    The solution is
    to add @SuppressLint("WrongConstant")** to the method.

References

  1. https://blog.csdn.net/wangwei890702/article/details/99644607
  2. android.content.Intent.java

Guess you like

Origin blog.csdn.net/wudexiaoade2008/article/details/116905007