隐式广播进行了限制

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:******************

如何应对这一限制

知道了上面的限制后,我们正常的应对方式为

  1. 能动态注册,就不静态注册
  2. 如果一定要静态注册, 发送的时候指定包名,即发送显式广播
  3. 如果要接收系统广播,而对应的广播在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了那就, 我们不希望这个判断能够进去。
那么如合让判断不进去呢?看下面代码。

  1.  
    1267 } else if (((r.intent.getFlags()&Intent.FLAG_RECEIVER_EXCLUDE_BACKGROUND) != 0)
  2.  
    1268 || (r.intent.getComponent() == null
  3.  
    1269 && r.intent.getPackage() == null
  4.  
    1270 && ((r.intent.getFlags()
  5.  
    1271 & Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND) == 0)
  6.  
    1272 && !isSignaturePerm(r.requiredPermissions))) {
  7.  
    1273 mService.addBackgroundCheckViolationLocked(r.intent.getAction(),
  8.  
    1274 component.getPackageName());
  9.  
    1275 Slog.w(TAG, "Background execution not allowed: receiving "
  10.  
    1276 + r.intent + " to "
  11.  
    1277 + component.flattenToShortString());
  12.  
    1278 skip = true;
  13.  
    1279 }
  1. 有这么个判断r.intent.getFlags()&Intent.FLAG_RECEIVER_EXCLUDE_BACKGROUND intent中携带了EXCLUDE_BACKGROUND标志位肯定进去,很显然我们正常都不带,只有希望后台收不到的时候才会带。
  2. r.intent.getComponent() == null, 这个肯定不会为null的。为null是必须跳过
  3. r.intent.getPackage() == null, 若包名为空,那肯定也跳过
  4. r.intent.getFlags() & Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND) == 0 不能带有FLAG_RECEIVER_INCLUDE_BACKGROUND这个标志位,若带了,那就进不去了,这不就是我们希望的结果么。

那么方案有了,携带 FLAG_RECEIVER_INCLUDE_BACKGROUND 这个标志位。我们发现在AS中使用Intent是找不到这个标志位的,应该是hide了,没有被编译进SDK。

看一下,果然,那么我们直接带硬编码即可。

  1.  
    /**
  2.  
    * If set, the broadcast will always go to manifest receivers in background (cached
  3.  
    * or not running) apps, regardless of whether that would be done by default. By
  4.  
    * default they will only receive broadcasts if the broadcast has specified an
  5.  
    * explicit component or package name.
  6.  
    *
  7.  
    * NOTE: dumpstate uses this flag numerically, so when its value is changed
  8.  
    * the broadcast code there must also be changed to match.
  9.  
    *
  10.  
    * @hide
  11.  
    */
  12.  
    public static final int FLAG_RECEIVER_INCLUDE_BACKGROUND = 0x01000000;

因此得出结论:发送广播的时候携带intent.addFlags(0x01000000); 即能让广播突破隐式广播限制

posted @ 2023-10-20 15:13  chlips  阅读(52)  评论(0编辑  收藏  举报