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:
- Dynamically register BroadcastReceiver in code;
- Send an explicit broadcast, that is, specify the package name and class name;
- Add flag Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND to Intent.
However, since FLAG_RECEIVER_INCLUDE_BACKGROUND is annotated with **@hide , the application layer code cannot find this variable (the code cannot be successfully compiled).
So it can only be hardcoded as intent.addFlags( 0x01000000 ).
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.
The solution is to add @SuppressLint("WrongConstant")** to the method.
References
- https://blog.csdn.net/wangwei890702/article/details/99644607
- android.content.Intent.java