【Android】:关于App开机自启动和禁止状态栏下拉的愚解

1.APP开机自启动:
首先声明所需要的权限:


然后在intent-filter中添加开机完成Action:


注意:
app开机自启动时需设置应用为主应用和默认应用,即在intent-filter中添加category为HOMEDEFAULT的配置



若应用未配置此两者则自启动无效。

2.禁止状态栏下拉:
法一(亲测可用):首先声明所需要的权限:


重写CustomViewGroup继承ViewGruop用于拦截手势事件:

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", "********** Status bar swipe intercepted");
        return true;
    }
}

定义一个新的CustomViewGroup:

protected CustomViewGroup blockingView = null;

定义一个函数用于实现状态栏拦截下拉:

protected void disableStatusBar() {

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

    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 receive touch events
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |

            // Draws over status bar
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

    localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
    localLayoutParams.height = (int) (40 * getResources().getDisplayMetrics().scaledDensity);
    localLayoutParams.format = PixelFormat.TRANSPARENT;

    blockingView = new CustomViewGroup(this);
    manager.addView(blockingView, localLayoutParams);
}

然后在主Activity的onCreate中调用disableStatusBar()

注意:使用此种方法会导致应用退出回到主界面时也无法对状态栏进行下拉,因为已经将状态栏给锁死了了,所以要对app进行退出监听,若应用进行了退出操作(即不杀死app回到主界面和杀死app都要进行监听),则将状态栏进行解锁,具体实现请有需求的同学自行补充

注:
法二:之前在查询百度和Google的时候有人推荐使用StatusBarManager这个类,但是这个类并不是android官方sdk所含有的类,需额外引用现有的类;
法三:这个方法改动比较多且不太友好,不太推荐,直接修改系统UI文件来达到禁止状态栏下拉的效果。

由于时间和篇幅及需求的关系,法二和法三并没有去具体实现测试过,有兴趣的同学可以尝试去做下。

活动

阿里云活动

你可能感兴趣的:(Android)