定制六:系统应用代码控制StatusBar和Navigation

2020-07-01  本文已影响0人  momxmo

前提条件:拥有系统权限

原理:利用反射设置系统属性;

作用:


import android.content.Context;

import java.lang.reflect.Method;

public class StatusBarUtils {

    public static final int DISABLE_EXPAND = 0x00010000;//4.2以上的整形标识
    public static final int DISABLE_EXPAND_LOW = 0x00000001;//4.2以下的整形标识
    public static final int DISABLE_NONE = 0x00000000;//取消StatusBar所有disable属性,即还原到最最原始状态

    public static final int DISABLE_NOTIFICATION_ICONS = 0x00020000;
    public static final int DISABLE_NOTIFICATION_ALERTS = 0x00040000;
    public static final int DISABLE_NOTIFICATION_TICKER = 0x00080000;
    public static final int DISABLE_SYSTEM_INFO     = 0x00100000;
    public static final int DISABLE_HOME        = 0x00200000;
    public static final int DISABLE_RECENT       = 0x01000000;
    public static final int DISABLE_BACK        = 0x00400000;
    public static final int DISABLE_CLOCK        = 0x00800000;
    public static final int DISABLE_SEARCH       = 0x02000000;
//    public static final int DISABLE_MASK = DISABLE_EXPAND | DISABLE_NOTIFICATION_ICONS
//            | DISABLE_NOTIFICATION_ALERTS | DISABLE_NOTIFICATION_TICKER
//            | DISABLE_SYSTEM_INFO | DISABLE_RECENT | DISABLE_HOME | DISABLE_BACK | DISABLE_CLOCK
//            | DISABLE_SEARCH;
//    public static final int DISABLE_MASK = DISABLE_EXPAND;
public static final int DISABLE_MASK = DISABLE_EXPAND | DISABLE_HOME | DISABLE_EXPAND | DISABLE_RECENT |DISABLE_BACK;

    public static void unBanStatusBar(Context context) {//利用反射解除状态栏禁止下拉
        Object service = context.getSystemService("statusbar");
        try {
            Class<?> statusBarManager = Class.forName
                    ("android.app.StatusBarManager");
            Method expand = statusBarManager.getMethod("disable", int.class);
            expand.invoke(service, DISABLE_NONE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void setStatusBarDisable(Context context,int disable_status) {//调用statusBar的disable方法
        Object service = context.getSystemService("statusbar");
        try {
            Class<?> statusBarManager = Class.forName
                    ("android.app.StatusBarManager");
            Method expand = statusBarManager.getMethod("disable", int.class);
            expand.invoke(service, disable_status);
        } catch (Exception e) {
            unBanStatusBar(context);
            e.printStackTrace();
        }
    }

    public static void banStatusBar(Context context) {//禁止statusbar下拉,适配了高低版本
        int currentApiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentApiVersion <= 16) {
            setStatusBarDisable(context,DISABLE_EXPAND_LOW);
        } else {
            setStatusBarDisable(context,DISABLE_MASK);
        }
    }
}

上一篇下一篇

猜你喜欢

热点阅读