Using root to clear the status bar notifications
android
Solution
UPDATE - See working solution below
All this stuff gets handled by the NotificationManagerService (see here: https://github.com/android/platform_frameworks_base/blob/master/services/java/com/android/server/NotificationManagerService.java). I think in particular you'd be interested in the void cancelAll(int userId) method. When you press clear on the status screen that's the method that actually gets invoked (with ActivityManager.getCurrentUser() as parameter).
You could try to obtain an instance of it by calling NotificationManager.getService via reflection (see hidden getService() method in NotificationManager http://androidxref.com/4.2.2_r1/xref/frameworks/base/core/java/android/app/NotificationManager.java) and then try to somehow invoke cancelAll on the returned service (e.g. via reflection again).
UPDATE
I found an easier way to clear notifications via the statusbar service. The following code should work:
IBinder b = (IBinder) Class.forName("android.os.ServiceManager").getMethod("getService", new Class[] {
String.class
}).invoke(null, new Object[] {
"statusbar"
});
Object iFace = Class.forName("com.android.internal.statusbar.IStatusBarService$Stub").getDeclaredMethod("asInterface", new Class[] {
IBinder.class
}).invoke(null, new Object[] {
b
});
iFace.getClass().getMethod("onClearAllNotifications", new Class[0]).invoke(iFace, (Object[]) null);
This will throw a SecurityException if you are not running as root but successfully clears notifications if you have root permissions
Problem
I am currently working on an app using an accessibility service to handle notifications. What is particularly annoying is there is no way for third party apps to clear the status bar notifications, other than launching the intent linked to the notification (and launch the app). I have searched for a long time for a way to use root to dismiss a notification or clear the complete list, but I have failed. I think I remember an app I saw that cleared the status bar by quickly opening the status bar and clicking the clear button programmatically, but I can't find it anymore, and I think it was on Android 2.2. I was wondering if there was a way to interact with the status bar notifications using some kind of database or with a simple SU call.