Home » Android » notifications – Android: How to collapse status bar on android 4.2?

notifications – Android: How to collapse status bar on android 4.2?

Posted by: admin June 16, 2020 Leave a comment

Questions:

Is there a way to collapse notification bar on Android 4.2?

I use this code that works only for the previous Android versions:

try{
            Object service  = c.getSystemService("statusbar");
            Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
            Method collapse = statusbarManager.getMethod("collapse");
            collapse.setAccessible(true);
            collapse.invoke(service);
        }catch(Exception ex){}

Please help me…

How to&Answers:

It’s hacky but since you have decided to tread that path, try to use the following code.

        if (currentApiVersion <= 16) {
            Method collapse = statusbarManager.getMethod("collapse");
            collapse.invoke(service);
        } else {
            Method collapse2 = statusbarManager.getMethod("collapsePanels");
            collapse2.invoke(service);
        }