Android,广播可分包数据

2023-12-27

我已经实现了一个扩展NotificationListenerService 的类,它可以很好地接收发布的通知。

然后我想获取收到的 statusBarNotification 对象并广播它。

我会做以下事情:

@Override
public void onNotificationPosted(StatusBarNotification statusBarNotification) {

    Intent intent = new Intent();
    intent.putExtra("STATUS_BAR_NOTIFICATION",statusBarNotification);
    intent.setAction("com.example.NotificationPosted");
    sendBroadcast(intent);
}

但是当我这样做时,我收到以下错误:

01-05 01:50:14.333  19574-19673/com.example W/NotificationListenerService[NotificationListener]﹕ Error running onNotificationPosted
java.lang.RuntimeException: Not allowed to write file descriptors here
        at android.os.Parcel.nativeAppendFrom(Native Method)
        at android.os.Parcel.appendFrom(Parcel.java:431)
        at android.os.Bundle.writeToParcel(Bundle.java:1679)
        at android.os.Parcel.writeBundle(Parcel.java:636)
        at android.app.Notification.writeToParcel(Notification.java:962)
        at android.service.notification.StatusBarNotification.writeToParcel(StatusBarNotification.java:106)
        at android.os.Parcel.writeParcelable(Parcel.java:1285)
        at android.os.Parcel.writeValue(Parcel.java:1204)
        at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
        at android.os.Bundle.writeToParcel(Bundle.java:1692)
        at android.os.Parcel.writeBundle(Parcel.java:636)
        at android.content.Intent.writeToParcel(Intent.java:7013)
        at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:2361)
        at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1127)
        at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:365)
        at com.example.NotificationListener.onNotificationPosted(NotificationListener.java:113)
        at android.service.notification.NotificationListenerService$INotificationListenerWrapper.onNotificationPosted(NotificationListenerService.java:168)
        at android.service.notification.INotificationListener$Stub.onTransact(INotificationListener.java:56)
        at android.os.Binder.execTransact(Binder.java:404)
        at dalvik.system.NativeStart.run(Native Method)

任何人都可以看到我做错了什么,或者这是不可能的。 StatusBarNotification 实现 Parcelable


我在使用 Twitter 的通知时也遇到了同样的问题。 我成功解决了这个问题,将通知的附加内容设置为空。

尝试这个:

@Override
@SuppressLint("NewApi") // Notification.extras is only available in API Level 19+
public void onNotificationPosted(StatusBarNotification statusBarNotification) {

    // Apprarently, the bug is caused by the extras when they're written to the parcel
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
         statusBarNotification.getNotification().extras = null;
    }
    Intent intent = new Intent();
    intent.putExtra("STATUS_BAR_NOTIFICATION",statusBarNotification);
    intent.setAction("com.example.NotificationPosted");
    sendBroadcast(intent);
}

请注意,如果您发送 contentIntent,此解决方案可能会破坏通知应用程序的功能(应用程序可能会认为额外内容存在而不进行检查)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android,广播可分包数据 的相关文章

随机推荐

  • 将 Python 脚本转换为 Ruby 时出错

    我正在使用一个Python脚本 它使用xmlrpclib import xmlrpclib srv xmlrpclib ServerProxy http demo myslice info 7080 allow none True auth
  • 有条件的预加载 - Laravel

    我有这样的疑问 tournament Tournament with championships championships settings championships category championships tree users
  • 将 C++/CLI 字符串数组转换为字符串向量

    我在 C CLI 中有一个参数 如下所示 array
  • 如何使用 HtmlAgilitypack 从头开始​​创建 html 文档

    我只想使用敏捷包创建自己的简单文档 因此创建一个新的 HtmlDocument 其中仅包含基本容器元素 即 我怎样才能从头开始执行此操作而不实际加载任何内容的 html 文档 更简单 var doc new HtmlDocument var
  • 如何使用Python OpenCV对苹果进行图像分割?

    我有浸泡在碘溶液中的苹果片的照片 目标是将苹果分割成各个感兴趣的区域并评估每个区域的淀粉水平 这是一个学校项目 所以我的目标是测试不同的分割方法 并客观地找到最佳解决方案 无论是单一技术还是多种技术的组合 问题是到目前为止我只接近一种方法
  • 加载带有西里尔字母符号的 url

    我必须加载一些带有西里尔字母符号的网址 我的脚本应该适用于此 http wincode org D0 BF D1 80 D0 BE D0 B3 D1 80 D0 B0 D0 BC D0 BC D0 B8 D1 80 D0 BE D0 B2
  • 解析带有冒号的字符串并将两边设置为不同的变量,C++

    因此 我试图编写一个简单的程序 该程序接受用户输入的时间 并计算模拟时钟指针之间和周围的角度 我已经运行了该程序 但没有解析和 2 个单独的输入 但我想解析冒号处的时间 例如 12 35 并将左侧设置为小时变量 将右侧设置为分钟变量 但是
  • 为什么 Win8 上 CRITICAL_SECTION 性能变得更差

    Windows 8 及更高版本上的 CRITICAL SECTION 性能似乎变得更差 见下图 测试非常简单 一些并发线程每个执行 300 万次锁定以独占方式访问变量 您可以在问题底部找到 C 程序 我在 Windows Vista Win
  • 文件系统除了文件和目录之外还有其他组件吗?

    我在视频教程中看到了这个 python 片段 它检查列出的项目是目录还是文件 for item in os listdir if os path isfile item do something elif os path isdir ite
  • Python - 使用子进程调用sed?

    我想打电话sed从 python 使用子进程 我尝试使用的脚本如下 但是 这会将 sed 输出传送到标准终端 看来我的 subprocess call 语句中无法识别 gt 运算符 有什么建议么 import sys import os i
  • NSRangeException:调用堆栈未显示行号

    我收到以下索引越界错误 Terminating app due to uncaught exception NSRangeException reason NSArrayM objectAtIndex index 0 beyond boun
  • 在 C# 中如何检查字符串变量是否为空或 null? [复制]

    这个问题在这里已经有答案了 如何检查 C 变量是否为空字符串 或空 我正在寻找最简单的方法来进行此检查 我有一个变量可以等于 或为空 是否有一个函数可以检查是否不存在 或空 if string IsNullOrEmpty myString
  • 如何在 Windows 7 上安装支持 Python 的 GDB

    我需要调试 cython 代码 官方文档说 我需要安装 gdb 7 2 或更高版本 使用 Python 支持构建 不幸的是 我没有找到任何如何在 Windows 7 上安装它的分步指南 我是否必须自己构建它 如何构建 或者已经存在适用于 W
  • Windows Installer 和 WiX 的创建

    我们目前使用 WiX 来构建 MSI 文件 因此它是我有过使用经验的唯一 MSI 构建器 我知道您可以在 Visual Studio 中本地构建安装程序 使用 WiX 和 Windows Installer 有什么区别 各自的优缺点是什么
  • Keycloak:验证访问令牌并获取 keycloak ID

    我需要能够执行以下操作 使用纯 cURL 和 JSON 服务器端 无框架或 Java 使用第 3 方提供的 Keycloak 访问令牌的字符串表示形式来验证令牌是否有效 如果令牌有效 则获取该用户的 Keycloak ID 如何使用普通的旧
  • 在viewpager中可以实现这种效果吗?

    我在用ViewPager in Activity 我需要在页面翻转之间产生这种效果 除了在 Android 的主屏幕之间切换之外 我从未见过这样的效果 如果您使用过这个动画或者您听说过动画的名字 请帮助我 我对这部动画没有任何想法 这种效应
  • 如何调试 linq to sql InsertOnSubmit 语句?

    以下代码可以正常工作 db DBUsers InsertOnSubmit new DBUser AllTheStuff valuesBeyondYourWildestDreams db SubmitChanges 我的猜测是数据库发生了一些
  • Ruby:过滤哈希键的最简单方法?

    我有一个看起来像这样的哈希 params irrelevant gt A String choice1 gt Oh look another one choice2 gt Even more strings choice3 gt But w
  • 使用curl获取标头

    为什么这不起作用 curl X HEAD http www google com 但这些都工作得很好 curl I http www google com curl X GET http www google com 您需要将 i 标志添加
  • Android,广播可分包数据

    我已经实现了一个扩展NotificationListenerService 的类 它可以很好地接收发布的通知 然后我想获取收到的 statusBarNotification 对象并广播它 我会做以下事情 Override public vo