android屏蔽状态栏下拉

来源:互联网 发布:js 严格模式 eval 编辑:程序博客网 时间:2024/05/17 00:11

网上关于屏蔽状态栏的文章搜到不少,但都是针对某个应用,或者锁屏状态,才能屏蔽状态栏的下拉,而我的需求是不管任意状态都屏蔽状态栏下拉,百度到的也可能版本不一样,说的一些文件都找不到,搜到一篇文章,自己修改了一个方法,然后OK了,具体如下:

文件位置:frameworks\base\core\java\android\app\StatusBarManager.java 

修改此方法中的参数,不用传穿进去的参数

  public void disable(int what) {         try {             mService.disable(DISABLE_EXPAND, mToken, mContext.getPackageName());         } catch (RemoteException ex) {             // system process is dead anyway.             throw new RuntimeException(ex);         }     }


参考文章:http://www.xuebuyuan.com/1608099.html

1.做锁屏软件,锁屏软件具体界面的实现不说,在屏蔽通知栏下拉的时候就出现问题了。网上找了一些资料,可以通过statusbarmanager这个类来实现,由于这个类是系统隐藏的,所以我们很容易就想到使用反射,这个类的源码如下:

 package android.app;  import android.content.Context; import android.os.Binder; import android.os.RemoteException; import android.os.IBinder; import android.os.ServiceManager;  public class StatusBarManager {     public static final int DISABLE_EXPAND = 0x00000001;     public static final int DISABLE_NOTIFICATION_ICONS = 0x00000002;     public static final int DISABLE_NOTIFICATION_ALERTS = 0x00000004;     public static final int DISABLE_NOTIFICATION_TICKER = 0x00000008;     public static final int DISABLE_NONE = 0x00000000;      private Context mContext;     private IStatusBar mService;     private IBinder mToken = new Binder();      StatusBarManager(Context context) {         mContext = context;         mService = IStatusBar.Stub.asInterface(                 ServiceManager.getService(Context.STATUS_BAR_SERVICE));     }         public void disable(int what) {         try {             mService.disable(what, mToken, mContext.getPackageName());         } catch (RemoteException ex) {             // system process is dead anyway.             throw new RuntimeException(ex);         }     }     public void expand() {         try {             mService.activate();         } catch (RemoteException ex) {             // system process is dead anyway.             throw new RuntimeException(ex);         }     }     public void collapse() {         try {             mService.deactivate();         } catch (RemoteException ex) {             // system process is dead anyway.             throw new RuntimeException(ex);         }     }     public void toggle() {         try {             mService.toggle();         } catch (RemoteException ex) {             // system process is dead anyway.             throw new RuntimeException(ex);         }     }      public IBinder addIcon(String slot, int iconId, int iconLevel) {         try {             return mService.addIcon(slot, mContext.getPackageName(), iconId, iconLevel);         } catch (RemoteException ex) {             // system process is dead anyway.             throw new RuntimeException(ex);         }     }      public void updateIcon(IBinder key, String slot, int iconId, int iconLevel) {         try {             mService.updateIcon(key, slot, mContext.getPackageName(), iconId, iconLevel);         } catch (RemoteException ex) {             // system process is dead anyway.             throw new RuntimeException(ex);         }     }      public void removeIcon(IBinder key) {         try {             mService.removeIcon(key);         } catch (RemoteException ex) {             // system process is dead anyway.             throw new RuntimeException(ex);         }     } }</span>

 

2.如果是系统级应用,也就是手机厂家植入的应用,可以使用disable(int)的方法来进行屏蔽,参数如上源码五个参数之一即可。但是如果是在应用层上的,disable方法因为权限问题无法使用(如果一定要使用必须具有系统签名)。这个时候可以使用collapse()方法,现在的小米锁屏和360锁屏都是使用该方法,具体代码如下:

<span style="margin: 0px; padding: 0px; border: 0px; font-size: 12px; background: transparent;">@Override    public void onWindowFocusChanged(boolean hasFocus) {        disableStatusBar();        super.onWindowFocusChanged(hasFocus);    }    public void disableStatusBar(){        try {            Object service = getSystemService("statusbar");            Class<?> claz = Class.forName("android.app.StatusBarManager");            Method expand = claz.getMethod("collapse");            expand.invoke(service);        } catch (Exception e) {            e.printStackTrace();        }    }</span>

 

3.

重写activity的onWindowfocuschanged方法,执行如上操作即可。以上方法使用了反射,如果不想使用使用反射获得隐藏的StatusBarManager,我这里提供一个jar包,将jar包导入到项目中,即可直接使用StatusBarManager ,还可以直接使用ServiceManager这个隐藏类,它有什么用?相信做过自动挂断电话的童鞋应该了解。

jar下载地址包:http://download.csdn.net/detail/welen123456789/5068165


0 0
原创粉丝点击