【问题标题】:Differentiate between PendingIntents sent by Notification actions区分 Notification 操作发送的 PendingIntents
【发布时间】:2014-09-17 11:06:49
【问题描述】:

我对 Android 开发相当陌生,并且正在开发一个依赖于在通知中放置一些是/否问题的应用程序。我将使用四个 PendingIntent:

  • 删除意图:记住问题已被驳回
  • 内容意图:打开一个提供相同问题的活动
  • 动作 1 的意图:回答是
  • 行动 2 的意图:回答“否”

我正在寻找的是告诉监听服务给出了哪个答案的最佳方式,而不必为他们定义额外的操作。我不喜欢在清单中乱扔一堆过于特定的意图过滤器。

显然,在 Intent 上设置 extra 是行不通的 - 这仍然会使框架将它们视为几乎相同的 Intent。

提供我正在寻找的功能的解决方案是使用

intent.setData(Uri.parse("answer://yes"));

intent.setData(Uri.parse("answer://no"));

但我不认为这就是意图数据的用途。

我想知道是否有人知道更好的解决方案,或者可以提供任何支持或反对我当前方法的论据。

【问题讨论】:

    标签: android android-intent android-notifications android-pendingintent


    【解决方案1】:

    显然,在 Intent 上设置 extra 是行不通的 - 这仍然会使框架将它们视为几乎相同的 Intent。

    如果您使用唯一整数作为 PendingIntent 工厂方法调用的第二个参数,它将起作用。您指出这些触发了“监听服务”,所以我假设您在 PendingIntent 上使用 getService() 作为您的工厂方法。

    另外,我建议将<intent-filter> 完全从<service> 中删除,并只使用额外的加上上面提到的唯一PendingIntent 标识符。如果您坚持使用<intent-filter>,请将服务标记为未导出(android:exported="false")。 Exported services relying upon implicit Intents are at risk of being spoofed,这就是为什么 L Developer Preview 不再允许隐含的 Intents 和 bindService()

    【讨论】:

    • 谢谢!我敢肯定,几年后,当我用谷歌搜索我自己的名字时,我会再次遇到这个问题,并且觉得问起来很愚蠢;-)。但今天,我很高兴找到了解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多