代码之家  ›  专栏  ›  技术社区  ›  Jason

确定挂起内容是否为活动

  •  3
  • Jason  · 技术社区  · 8 年前

    有没有办法判断 PendingIntent 或 IntentSender 是否会启动活动(相对于广播或服务)?

    处理意图 具有 isActivity() 函数,但标记为 @hide .

    我已尝试从中复制/粘贴代码 here 但无济于事。

    1 回复  |  直到 8 年前
        1
  •  2
  •   David Wasser    8 年前

    A. PendingIntent 只是对实际情况的参考 Intent . 这个 处理意图 实际上不包含任何数据,因此您无法查看它。实际情况 意图 由Android本身管理。不幸的是,你无法获得你想要的信息。

    注:以上答案不正确。从Android 4.1开始,可以确定 意图 由a包裹 处理意图 将启动 Activity .

    您可以使用反射进行操作,如下所示:

        PendingIntent pendingIntent = ... // This is the `PendingIntent`
        try {
            Method method = PendingIntent.class.getDeclaredMethod("isActivity");
            method.setAccessible(true);
            Boolean isActivity = m.invoke(pendingIntent);
            if (isActivity) {
                // PendingIntent will launch an Activity
            }
        } catch (Exception e) {
            // Problem using reflection
        }
    

    在早于4.1的Android版本上,这将引发 NoSuchMethodException .

    调用(私有)方法 isActivity() 上 处理意图 生成对Android的调用 ActivityManager ,询问其 处理意图 是一个 活动 .