关于第三方apk如何禁用systemUI的statusbar

最近一个国外的客户叫我教他们开发一款apk,目的是屏蔽系统的statusbar。在百度上搜索了一堆都是要用到系统签名和获取系统的权限,这些对第三方apk来说是不实际的!

通过各方搜索后,在一个国外网站看到了一个方法,可以直接使用,觉的比较好,分享给大家下:

首先:在自己的activity中写一个窗口,覆盖状态栏上以禁止状态栏下拉,代码如下:

public static void preventStatusBarExpansion(Context context) {

WindowManager manager = ((WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE));



Activity activity = (Activity) context;

WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();

localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;

localLayoutParams.gravity = Gravity.TOP;

localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |



// this is to enable the notification to recieve touch events

WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |



// Draws over status bar

WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;



localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;

int resId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");

int result = 0;

if (resId > 0) {

result = activity.getResources().getDimensionPixelSize(resId);

}



localLayoutParams.height = result;



localLayoutParams.format = PixelFormat.TRANSPARENT;



View view = new customViewGroup(context);



manager.addView(view, localLayoutParams);

}



public static class customViewGroup extends ViewGroup {



public customViewGroup(Context context) {

super(context);

}



@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

}



@Override

public boolean onInterceptTouchEvent(MotionEvent ev) {

Log.v("customViewGroup", "**********Intercepted");

return true;

}

}

其次:在onWindowFocusChanged,onResume,onPause加入判断;

public void onWindowFocusChanged(boolean hasFocus) {

currentFocus = hasFocus;

if (!hasFocus) {

collapseNow();

}

super.onWindowFocusChanged(hasFocus);

}

protected void onResume() {

isPaused = false;

super.onResume();

}

protected void onPause() {

isPaused = true;

super.onPause();

}

private void collapseNow() {

if (collapseNotificationHandler == null) {

collapseNotificationHandler = new Handler();

}



if (!currentFocus && !isPaused) {

collapseNotificationHandler.postDelayed(new Runnable() {



@Override

public void run() {

Object statusBarService = getSystemService("statusbar");

Class<?> statusBarManager = null;



try {

statusBarManager = Class.forName("android.app.StatusBarManager");

} catch (ClassNotFoundException e) {

e.printStackTrace();

}


//这边的method方法导入的包是import java.lang.reflect.Method;

Method collapseStatusBar = null;



try {

if (Build.VERSION.SDK_INT > 16) {

collapseStatusBar = statusBarManager.getMethod("collapsePanels");

} else {

collapseStatusBar = statusBarManager.getMethod("collapse");

}

} catch (NoSuchMethodException e) {



}



collapseStatusBar.setAccessible(true);



try {

collapseStatusBar.invoke(statusBarService);

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}



if (!currentFocus && !isPaused) {

collapseNotificationHandler.postDelayed(this, 10L);

}

}

}, 10L);

}

}

最后:在自己的activity中加上:

private Handler collapseNotificationHandler;

private boolean currentFocus;

private boolean isPaused;

最后的最后:别忘记了在AndroidManifest.xml加上权限

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

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

至此,你编译出来的apk就可以屏蔽系统的通知栏下拉了。(亲测在android 4.4 系统上有效)!

希望以上方法可以帮助需要的人!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值