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

思路: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、通知栏、消息中心)

欢迎关注微信公众号:DroidMind
精品内容独家发布平台


呈现与博客不一样的技术干货

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值