美文网首页
定制六:系统应用代码控制StatusBar和Navigation

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

作者: momxmo | 来源:发表于2020-07-01 14:42 被阅读0次

    前提条件:拥有系统权限

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

    作用:

    • 显示隐藏导航栏的home、返回、最近任务、音量、截图等功能按钮;
    • 禁止和解除状态栏下拉等;
    
    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);
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:定制六:系统应用代码控制StatusBar和Navigation

          本文链接:https://www.haomeiwen.com/subject/jdncqktx.html