zoukankan      html  css  js  c++  java
  • Android高版本收不到静态注册的广播

    Beginning with Android 8.0 (API level 26), the system imposes additional restrictions on manifest-declared receivers.
    If your app targets Android 8.0 or higher, you cannot use the manifest to declare a receiver for most implicit broadcasts (broadcasts that don't target your app specifically). You can still use a context-registered receiver when the user is actively using your app.

    Android为了防止静态注册的广播耗费资源,禁用了一些在Manifest文件中注册的广播。导致收不到消息,在logcat中能看到类似这样的日志

    2019-10-08 00:02:29.523 350-365/system_process W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.BATTERY_LOW flg=0x4000010 (has extras) } to com.hncj.android/.PowerStateReceiver

    说明广播被系统拦截了.

    这里是仍然可以使用静态注册的系统广播:
    https://developer.android.google.cn/guide/components/broadcast-exceptions?hl=en

    解决方法:
    1、想要接收系统广播,似乎只有降低targetSdkVersion为25或以下,但此种方式Manifest文件会报错,需要在前面加一句:
    //noinspection ExpiredTargetSdkVersion
    targetSdkVersion 25
    

    降低targetSdkVersion后,项目也会编译报错,找不到一些类,资源、样式等文件。需要删除build.gradle中的:

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    

    同时activity类不要继承AppCompatActivity,直接继承Activity.酌情删除或修改Manifest.xml中的theme、roundIcon等样式,酌情删除一些图片,变量定义文件。

    2、对于自定义的广播,想要使用静态注册,网上也提供了解决方案:

    如:在sendBroadcast方法之前,调用intent的setPackage或setComponent方法,为其设置具体的接收者;

    或者使用intent的addFlag方法,阻止系统拦截广播

    intent.addFlags(0x01000000);
    

    详细可参考: https://www.jianshu.com/p/5283ebc225d5?utm_source=oschina-app

  • 相关阅读:
    jquery判断复选框是否选中
    jquery验证网址格式
    jquery右下角返回顶部
    thinkphp分页格式的完全自定义,直接输入数字go到输入数字页
    textarea出现多余的空格
    html渐隐轮播
    linux 路由 route
    ansible 自动化运维工具
    数据库 group by 后其他列的取值
    linux 磁盘io高排查
  • 原文地址:https://www.cnblogs.com/wotoufahaiduo/p/11633219.html
Copyright © 2011-2022 走看看