打开时关闭状态栏:Android [英] Close status bar when opened: Android

查看:24
本文介绍了打开时关闭状态栏:Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图想出一些方法来禁用状态栏而不完全隐藏它.这是在 3rd 方应用程序中禁用状态栏的介绍尝试.目前,我想在我自己的应用程序中禁用它,然后最终创建一个后台服务,看看我是否可以在其他应用程序中这样做.我正在创建的应用程序是一个面向儿童的操作系统,我正在尝试开发一个封闭的系统.

I am trying to come up with some ways of disabling the status bar without hiding it completely. This is an introlude attempt at disabling status bars in 3rd party apps. For now, I want to disable it in my own app, and then eventually create a background service to see if I can do so in other apps. The app I am creating is an operating system for children, and I am trying to develop a closed system.

这是我尝试过的.我最初的想法是监控状态栏何时被访问,然后关闭它.

Here is what I have tried. My initial idea was to monitor when status bar was accessed and then closing it.

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    Log.i(TAG, "onWindowFocusChanged()");
    try {
        if (!hasFocus) {
            Log.d(TAG, "close status bar attempt");
            Object service = getSystemService("statusbar");
            Class<?> statusbarManager = Class
                    .forName("android.app.StatusBarManager");
            Method collapse = statusbarManager.getMethod("collapse");
            collapse.setAccessible(true);
            collapse.invoke(service);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

这是我使用的方法.它用于检测何时访问状态栏,但是,一旦获得焦点,它就不会关闭状态栏.我错过了什么?提前致谢.

This is the method I used. It works for detecting when status bar is being accessed, however, it does not close the status bar once it has focus. What am I missing? Thanks in advance.

推荐答案

我找到了问题的答案.首先,我缺少以下权限

I have found answer to my question. First, I was missing the following permission

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

有了这个权限,下面的代码现在工作得很好.

With that permission, the following code now works really well.

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    Log.i(TAG, "onWindowFocusChanged()");
    try {
        if (!hasFocus) {
            Log.d(TAG, "close status bar attempt");
            //option 1
            int currentApiVersion = android.os.Build.VERSION.SDK_INT;
            Object service = getSystemService("statusbar");
            Class<?> statusbarManager = Class
                    .forName("android.app.StatusBarManager");

            if (currentApiVersion <= 16) {
                Method collapse = statusbarManager.getMethod("collapse");
                collapse.setAccessible(true);
                collapse.invoke(service);
            } else {
                Method collapse = statusbarManager.getMethod("collapsePanels");
                collapse.setAccessible(true);
                collapse.invoke(service);
            }
            // option 2
            Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            mContext.sendBroadcast(it); 

        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

如果您注意到了,我发现还有第二个选项效果很好.如果要使用选项 2,可以注释掉选项 1,反之亦然.两者都完成相同的事情,尽管我认为选项 2 更好.

If you notice, there is a second option as well that I found to be working good. You can comment out option 1 if you want to use option 2, or vise versa. Both accomplish the same thing, although I believe option 2 is better.

Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
mContext.sendBroadcast(it); 

我发现唯一的缺点是关闭时它很慢(呃).但是,这两种方法的崩溃速度都足够快,以至于没有人可以单击状态栏中的任何通知或选项.希望这对其他人有帮助.祝你好运,干杯!

The only downfall I found is that it is slow(er) when closing. However, both methods collapse quick enough to where no one can click on any notifications or options in the status bar. Hopefully this is helpful to someone else. Good luck, Cheers!

这篇关于打开时关闭状态栏:Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆