2

Я пытался отменить уведомления целевого приложения на Android, затем я прочитал исходный код и заметил, что для этого могут быть некоторые хитрости. Я попытался назвать это отражением.Как вызвать метод cancelAllNotifications (String) (в android.app.INotificationManager) путем отражения

public abstract interface INotificationManager extends IInterface { 

    public abstract void cancelAllNotifications(String paramString) throws RemoteException; 

но не работает хорошо, продолжайте бросать InvocationTargetException, когда код запускается до последней строки. Я до сих пор не понимаю почему. Не могли бы вы дать мне несколько намеков?

это мой код. Большое спасибо :)

 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"}); 

ответ

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(); 
    } 
+0

это не будет работать, если вы пытаетесь вызвать этот метод. InvocationTargetException все равно случится. :) –