3

Is there a way to tell if PendingIntent or IntentSender will launch an activity (as opposed to a broadcast or service)?

PendingIntent has an isActivity() function, but it's marked as @hide.

I have tried to copy/paste the code in from here but to no avail.

Bishoy Kamel
  • 2,327
  • 2
  • 17
  • 29
Jason
  • 13,563
  • 15
  • 74
  • 125
  • I'm not a big fan of this, but reflection might be way to go here: https://stackoverflow.com/q/880365/794088 – petey Jan 29 '18 at 20:55
  • See https://stackoverflow.com/questions/7896672/how-to-launch-a-new-activity-using-pending-intent – Mihai8 Jan 29 '18 at 22:58
  • for what purpose? are you trying to distinguish between `PendingIntent` internal to your application? – payloc91 Jan 30 '18 at 10:57
  • 1
    We are given PendingIntents by the NotificationListenerService and trying to figure out which will launch apps and which will not. For instance, "Like" on a Facebook notification will not launch the app, whereas hitting "reply" on Gmail will. – Jason Jan 30 '18 at 12:43

1 Answers1

2

A PendingIntent is just a reference to the actual Intent. The PendingIntent doesn't actually contain any of the data, so you cannot look at it. The actual Intent is managed by Android itself. There's unfortunately no way you can get the information you want.

NOTE: The above answer is not correct. Starting with Android 4.1, it is possible to determine if the Intent wrapped by a PendingIntent will launch an Activity.

You can do it using reflection, like this:

    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
    }

On versions of Android older than 4.1 this will throw a NoSuchMethodException.

Calling the (private) method isActivity() on the PendingIntent generates a call to the Android ActivityManager, asking it if the target of the PendingIntent is an Activity.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 1
    You are absolutely correct! Thank you so much for your comment. I love being proved wrong. And I have learned something new today. I was looking at an older version of the Android source code where this method does not exist! `isActivity()` is available from Android 4.1. In older versions the method does not exist. I really appreciate this. – David Wasser Feb 01 '18 at 09:41
  • you might want to remove the first paragraph. Some people stop reading when they think they have the answer – Tim Feb 01 '18 at 09:56
  • @TimCastelijns I thought about that, but then the comment thread makes no sense. Part of the first paragraph is actually correct, in that the `PendingIntent` really doesn't have this information. The `isActivity()` method has to go ask the `ActivityManager` for the answer. I'm open to suggestions about how to improve the answer, but I don't think removing the original answer it the best way to do it. – David Wasser Feb 01 '18 at 10:00