2

I was trying to cancel target app's notifications on Android, then I read the source code and noticed it may have some trickies to do that. I tried to call this by reflection.

public abstract interface INotificationManager extends IInterface {

    public abstract void cancelAllNotifications(String paramString) throws RemoteException;

but not worked well, keep throwing InvocationTargetException when the code run to the last line. I still don't get why. Could you give me some hints please ?

this is my code . Thanks a lot :)

        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Method getServiceMethod = mNotificationManager.getClass().getMethod("getService");

        Object iNotificationManagerObject = getServiceMethod.invoke(mNotificationManager, (Object[]) null);

        Class iNotificationManager = Class.forName("android.app.INotificationManager");

        Method cancelAllNotificationsMethod = iNotificationManager.getMethod("cancelAllNotifications", new Class[] { String.class});
        cancelAllNotificationsMethod.invoke(iNotificationManagerObject, new Object[] { "com.reallyBadApp"});
Bruce_Van
  • 46
  • 5

1 Answers1

0
try {
        Class<?> c = Class.forName("android.app.NotificationManager");
        Method method = c.getMethod("getService");
        Object obj = method.invoke(mNotificationManager);
        Class<?> clazz = Class.forName("android.app.INotificationManager$Stub$Proxy");
        if (VERSION.SDK_INT < 17) {
            Method cancelAllNotificationsMethod = clazz.getMethod("cancelAllNotifications", String.class);
            cancelAllNotificationsMethod.invoke(obj, pkg);
        } else {
            Method cancelAllNotificationsMethod = clazz.getMethod("cancelAllNotifications", String.class, int.class);
            cancelAllNotificationsMethod.invoke(obj, pkg, 0);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
Li Stefan
  • 1
  • 1