【问题标题】:Android receivers - Same name and action of receiver in multiple appsAndroid 接收器 - 多个应用程序中接收器的名称和操作相同
【发布时间】:2015-12-07 09:05:44
【问题描述】:

假设我在 2 个应用程序(应用程序 A 和应用程序 B)的清单中有以下接收器:

<receiver android:enabled="true" android:name="com.MyReceiver">
    <intent-filter>
        <action android:name="com.COMMON_ACTION" />
    </intent-filter>
</receiver>

在每个应用程序中,如果不存在,我想创建一个PendingIntent,并使用AlarmManager 将其设置为不精确重复。要检查是否存在,我执行以下代码:

boolean alarmExists = (PendingIntent.getBroadcast(mContext,
            DEFAULT_PENDING_INTENT_ID, intent,
            PendingIntent.FLAG_NO_CREATE) != null);

这是否应该在 App A 中返回 false,即使 App B 已经在同一设备上创建了待处理 Intent? 是否有理由推迟两个应用中的接收者(通过对每个应用使用不同的操作)?

【问题讨论】:

    标签: android broadcastreceiver android-pendingintent


    【解决方案1】:

    每个应用程序都有自己的PendingIntents。这些不会在不同的应用程序之间共享。

    如果应用程序 A 使用 ACTION="com.COMMON_ACTION" 创建了 PendingIntent,而应用程序 B 这样做:

    Intent intent = new Intent("com.COMMON_ACTION");
    boolean alarmExists = (PendingIntent.getBroadcast(mContext,
            DEFAULT_PENDING_INTENT_ID, intent,
            PendingIntent.FLAG_NO_CREATE) != null);
    

    alarmExists 将是 false

    【讨论】:

    • 谢谢。你能指出我一些文档吗?所以基本上,意图过滤器也会检查创建者包名称?
    • 是的,确实如此。不幸的是,这在文档中没有明确指出。你可以通过自己测试和阅读大量源代码来学习这些东西;-) 你需要 20 分钟来编写一个小测试程序来自己测试。
    猜你喜欢
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多