Get Intent from PendingIntent causes SecurityExcep

2019-07-13 18:35发布

问题:

We have some old code that worked for a long time:

public static Intent getIntent(PendingIntent pendingIntent) {
    Intent intent = null;
    try {
        Method getIntent = PendingIntent.class.getDeclaredMethod("getIntent");
        intent = (Intent) getIntent.invoke(pendingIntent);
    } catch (Exception e) {
        // Log line
    }
    return intent;
}

We are now getting a security exception:

java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at com.company.util.IntentUtils.getIntent(IntentUtils.java:160)
Caused by: java.lang.SecurityException: Permission Denial: getIntentForIntentSender() from pid=28128, uid=10127 requires android.permission.GET_INTENT_SENDER_INTENT
    at android.os.Parcel.readException(Parcel.java:1683)
    at android.os.Parcel.readException(Parcel.java:1636)
    at android.app.ActivityManagerProxy.getIntentForIntentSender(ActivityManagerNative.java:5924)
    at android.app.PendingIntent.getIntent(PendingIntent.java:987)
    ... 17 more

Looks like it might be related to AOSP change: https://android.googlesource.com/platform/frameworks/base/+/e5ad41b%5E!/

Any help getting the intend would be appreciated.

回答1:

There's no workaround: as per the error message and commit, that hidden method that was never part of the public API now requires a signature permission that only apps that are signed with the system signature can hold.