1

我无法使用 android 4.2 扩展状态栏 no my nexus 4,但其他级别运行正常,我的代码是:

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) {
    }


}

我能怎么做 ?

4

2 回答 2

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) {
    }

}

这样就可以正常运行了!

于 2013-05-10T06:53:48.433 回答
0

这是我的做法,它确实有效:

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
}

您应该检查究竟是哪个异常被抛出(可能是这种情况)并找出确切的原因。

于 2013-05-08T11:20:10.000 回答