Using root to clear the status bar notifications

后端 未结 2 1270
忘了有多久
忘了有多久 2021-02-08 07:24

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 part

相关标签:
2条回答
  • 2021-02-08 07:38

    You can clear all the notifications by calling

    service call notification 1
    

    in the shell with root permission

    0 讨论(0)
  • 2021-02-08 07:50

    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

    0 讨论(0)
提交回复
热议问题