Android 4.0屏蔽状态栏(已解决)

来源:互联网 发布:淘宝客邮件推广 编辑:程序博客网 时间:2024/05/16 17:17

众所周知,安卓4.0以后安全性提高很多,很多之前在2.2的方法,在4.0都实效了。

网上关于屏蔽状态栏的方法很多,以用反射的方法比较多(因为app层没有直接公开可以操控状态栏的类,所以要通过反射),大致如下

 try{            Object service = getSystemService("statusbar");            Class<?> statusBarManager = Class.forName("android.app.StatusBarManager");            Method expand = statusBarManager.getMethod("collapse");            expand.invoke(service);        }catch(Exception e)        {                }

很多人说这个方法效果不好,我试了下效果确实也不好。于是查看了下API版本号为14的StatusBarManager源码,在总共160多行的代码中,看到了collapse和expand方法,分别对应折叠和展开状态栏,也就是上面代码中的折叠:getMethod("collapse")。

/**     * 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);        }    }


既然源码都是这么写的,说明安卓的状态栏就是按照这个方法来折叠/展开状态栏的,那为什么我们平时还感觉不行呢?

于是我想到了线程,每隔一定的时间执行上面的代码,那么不就可以使用户点不动了吗?!事实证明这个方法确实奏效了(不过貌似有点小小的问题)!

测试后发现,把线程的休眠时间设置为100ms,已经不容易拉下来。当设置为10ms时,基本拉不下来了(不知道这样是不是巨浪费系统资源)。效果达到!

注意:参考了API版本18,发现上面的collapse变成了collapsePanels(),具体的请参考源码。

注意添加权限

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


0 0
原创粉丝点击