第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > android锁屏软件屏蔽状态栏下拉

android锁屏软件屏蔽状态栏下拉

时间:2021-09-05 02:01:49

相关推荐

android锁屏软件屏蔽状态栏下拉

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

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

@Overridepublic 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();}}

3.

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

jar下载地址包:/detail/welen123456789/5068165

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。