我试图在Android上取消目标应用的通知,然后我阅读了源代码并注意到它可能有一些技巧。 我试图通过反思来称呼它。
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 :(得分: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();
}