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

  • 相关阅读:
    网页制作初期,必须的东西
    网页制作知识100问(五)
    打開新窗口
    [转]如何用Delphi开发网游外挂
    钩子技术
    [转]计算两点间的角度
    [转]快速寻找子位图的位置
    (转)Delphi读写UTF8、Unicode格式文本文件
    GridView中控件列使用方法小结
    ASP.NET2.0 生成Word 2007并下载方案
  • 原文地址:https://www.cnblogs.com/wotoufahaiduo/p/11633219.html
Copyright © 2011-2022 走看看