1

I can not expand statusbar no my nexus 4 with android 4.2,but other level is running OK ,My code is:

public void OpenNotify() {
    // TODO Auto-generated method stub
    int currentApiVersion = android.os.Build.VERSION.SDK_INT;
    try {
        Object service  = getSystemService("statusbar");
        Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");

        if (service != null) {
            /*Method expand = service.getClass()statusbarManager.getMethod("expand");
            expand.invoke(service);*/
            if (currentApiVersion <= 16) {
                Method collapse = statusbarManager.getMethod("collapse");
                collapse.setAccessible(true);
                collapse.invoke(service);
            } else {
                Method collapse2 = statusbarManager.getMethod("collapsePanels");
                collapse2.setAccessible(true);
                collapse2.invoke(service);
            }
        }

    } catch (Exception e) {
    }


}

And

How can I do ?

2 Answers2

2
public void OpenNotify() {
    // TODO Auto-generated method stub
    int currentApiVersion = android.os.Build.VERSION.SDK_INT;
    try {
        Object service = getSystemService("statusbar");
        Class<?> statusbarManager = Class
                .forName("android.app.StatusBarManager");
        Method expand = null;
        if (service != null) {
            if (currentApiVersion <= 16) {
                expand = statusbarManager.getMethod("expand");
            } else {
                expand = statusbarManager
                        .getMethod("expandNotificationsPanel");
            }
            expand.setAccessible(true);
            expand.invoke(service);
        }

    } catch (Exception e) {
    }

}

In this way ,it runs OK !

0

Here's how I'm doing it and it actually works:

private static final String collapse_method = Build.VERSION.SDK_INT > 16
    ? "collapsePanels"
    : "collapse";
try {
    Object obj = context.getSystemService("statusbar");
    Class.forName("android.app.StatusBarManager")
            .getMethod(collapse_method, new Class[0])
            .invoke(obj, (Object[]) null);
} catch (Exception e) {
    Log.e("STATUSBAR", "Failed to collapse status panel: "+e);
    // do nothing, it's OK
}

You should check which exactly exception was thrown (as it is possibly the case) and figure out the exact reason.

aragaer
  • 17,238
  • 6
  • 47
  • 49
  • In this way,you want to expands statusbar or collapse the statusbar? I think I take a mistake ,I want to expand it ,there are some different between them ,And how can I do ,I have ran your code ,It is not work to me. – user2309540 May 08 '13 at 11:35
  • Now that is strange since it runs right now on my Nexus 4 with android 4.2. What you see in the logs? Which exception gets thrown? – aragaer May 08 '13 at 12:12