Android O對隱式廣播進行了限制, 其限制鏈接說明: https://developer.android.com/about/versions/oreo/background
上面所說即:若App的TargetSDK達到了26, 我們正常靜態注冊的廣播就沒有用了。能用的僅有以下豁免的Broadcast, 包括我們自己正常發廣播,如果不指定包名, 靜態注冊的也是收不到的。PS:動態注冊是沒有影響的
https://developer.android.com/guide/components/broadcast-exceptions
在我們收不到廣播的時候,系統會有如下打印,即這個后臺的廣播接收器不會被執行
04-21 04:12:27.513 2431 4821 W BroadcastQueue: Background execution not allowed:******************
如何應對這一限制
知道了上面的限制后,我們正常的應對方式為
- 能動態注冊,就不靜態注冊
- 如果一定要靜態注冊, 發送的時候指定包名,即發送顯式廣播
- 如果要接收系統廣播,而對應的廣播在Android8.0中無法被接收,那么只能暫時把App的targetSdkVersion改為25或以下,但這招已經不頂用了,工信部要求targetSDK必須26以上
如果我們不想發顯式廣播(因為我們不知道有誰要收廣播),對方又不能動態注冊,只能靜態注冊(許多應用希望是被動喚醒),我們應該怎么辦呢?
我們看上面的異常:
04-21 04:12:27.513 2431 4821 W BroadcastQueue: Background execution not allowed:******************
這行log是哪邊打印的呢?
我們去搜索一下:http://androidxref.com/
其代碼在:frameworks/base/services/core/java/com/android/server/am/BroadcastQueue.java#1275
代碼里面有個skip變量是用來標志是否跳過的,很顯然1275行打印出來了,skip為true了那就, 我們不希望這個判斷能夠進去。
那么如合讓判斷不進去呢?看下面代碼。
1267 } else if (((r.intent.getFlags()&Intent.FLAG_RECEIVER_EXCLUDE_BACKGROUND) != 0)
1268 || (r.intent.getComponent() == null
1269 && r.intent.getPackage() == null
1270 && ((r.intent.getFlags()
1271 & Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND) == 0)
1272 && !isSignaturePerm(r.requiredPermissions))) {
1273 mService.addBackgroundCheckViolationLocked(r.intent.getAction(),
1274 component.getPackageName());
1275 Slog.w(TAG, "Background execution not allowed: receiving "
1276 + r.intent + " to "
1277 + component.flattenToShortString());
1278 skip = true;
1279 }
- 有這么個判斷
r.intent.getFlags()&Intent.FLAG_RECEIVER_EXCLUDE_BACKGROUND
intent中攜帶了EXCLUDE_BACKGROUND
標志位肯定進去,很顯然我們正常都不帶,只有希望后臺收不到的時候才會帶。 -
r.intent.getComponent() == null
, 這個肯定不會為null的。為null是必須跳過 -
r.intent.getPackage() == null
, 若包名為空,那肯定也跳過 -
r.intent.getFlags() & Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND) == 0
不能帶有FLAG_RECEIVER_INCLUDE_BACKGROUND
這個標志位,若帶了,那就進不去了,這不就是我們希望的結果么。
那么方案有了,攜帶 FLAG_RECEIVER_INCLUDE_BACKGROUND 這個標志位。我們發現在AS中使用Intent是找不到這個標志位的,應該是hide了,沒有被編譯進SDK。
看一下,果然,那么我們直接帶硬編碼即可。
/**
* If set, the broadcast will always go to manifest receivers in background (cached
* or not running) apps, regardless of whether that would be done by default. By
* default they will only receive broadcasts if the broadcast has specified an
* explicit component or package name.
*
* NOTE: dumpstate uses this flag numerically, so when its value is changed
* the broadcast code there must also be changed to match.
*
* @hide
*/
public static final int FLAG_RECEIVER_INCLUDE_BACKGROUND = 0x01000000;
因此得出結論:發送廣播的時候攜帶intent.addFlags(0x01000000);
即能讓廣播突破隱式廣播限制。
謝謝大家的閱讀。
本文作者:Anderson/Jerey_Jobs
博客地址 : http://jerey.cn/
簡書地址 : Anderson大碼渣
github地址 : https://github.com/Jerey-Jobs