Search code examples
androidandroid-activityandroid-pendingintent

Determine if PendingIntent is an Activity


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.


Solution

  • 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.