用代码打开通知中心(statusbar、通知栏、消息中心)

来源:互联网 发布:小学生机器人编程教程 编辑:程序博客网 时间:2024/06/05 15:12

思路:API中没有实现的方法,那么就利用反射机制

问题:4.2系统中的方法变更

解决办法:分系统实现不同的方法

源码路径:……\sdk\sources\android-18\android\app\StatusBarManager

我们先来看android 4.4(API 19)中的方法,android 4.3(API 18),android 4.2.2(API 17)中都是下列方法

    /**     * Expand the notifications panel.     */    public void expandNotificationsPanel() {        try {            final IStatusBarService svc = getService();            if (svc != null) {                svc.expandNotificationsPanel();            }        } catch (RemoteException ex) {            // system process is dead anyway.            throw new RuntimeException(ex);        }    }    /**     * Collapse the notifications and settings panels.     */    public void collapsePanels() {        try {            final IStatusBarService svc = getService();            if (svc != null) {                svc.collapsePanels();            }        } catch (RemoteException ex) {            // system process is dead anyway.            throw new RuntimeException(ex);        }    }

我们再来看android 4.1.2(API 16)中的方法,终于发现了方法名的不同了。

/**     * Expand the status bar.     */    public void expand() {        try {            final IStatusBarService svc = getService();            if (svc != null) {                svc.expand();            }        } catch (RemoteException ex) {            // system process is dead anyway.            throw new RuntimeException(ex);        }    }    /**     * Collapse the status bar.     */    public void collapse() {        try {            final IStatusBarService svc = getService();            if (svc != null) {                svc.collapse();            }        } catch (RemoteException ex) {            // system process is dead anyway.            throw new RuntimeException(ex);        }    }

找到问题后我们就需要用反射机制和判断系统版本的方式来写代码了。

/**     * 显示消息中心     */    public static void openStatusBar(Context mContext){        try {                  Object service = mContext.getSystemService ("statusbar");                  System.out.println("SDK INT= "+VERSION.SDK_INT                       +" BUILD.VERSION.SDK"+Build.VERSION.SDK_INT);               Class <?> statusBarManager = Class.forName("android.app.StatusBarManager");                // 判断系统版本号               String methodName = (VERSION.SDK_INT<=16)?"expand":"expandNotificationsPanel";               Method expand = statusBarManager.getMethod (methodName);                  expand.invoke (service);           }         catch (Exception e) {              e.printStackTrace();        }     }    /**     * 关闭消息中心     */    public static void closeStatusBar(Context mContext){        try {                  Object service = mContext.getSystemService ("statusbar");                  Class <?> statusBarManager = Class.forName("android.app.StatusBarManager");               // 判断系统版本号               String methodName = (VERSION.SDK_INT<=16)?"collapse":"collapsePanels";               Method expand = statusBarManager.getMethod(methodName);                  expand.invoke(service);          }         catch (Exception e) {              e.printStackTrace();        }     }

使用权限:

<uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>

原文链接:用代码打开通知中心(statusbar、通知栏、消息中心)

0 0
原创粉丝点击