如何在Android中禁用状态栏的单击和下拉? [英] How to disable status bar click and pull down in Android?

查看:80
本文介绍了如何在Android中禁用状态栏的单击和下拉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Android中禁用状态栏的单击和下拉?我已经尝试了很多东西,但这没有用.

How can I disable a status bar click and pull down in Android? I have tried many things but this is not working.

推荐答案

我认为有2种选择:

选项1:您可以在状态栏上放置一个窗口,以禁止任何触摸或下拉.

OPTION 1: You can lay a window over the status bar to disable any touch or pulling down.

选项2:您还可以重写OnWindowFocusChanged()方法,以在显示通知面板后立即关闭它.

OPTION 2: You can also override the OnWindowFocusChanged() method to close the notification panel immediately after it shows up.

方法:

选项1:在您的活动中定义以下方法(preventStatusBarExpansion)和类(CustomViewGroup).

OPTION 1: Define the following method(preventStatusBarExpansion) and class(CustomViewGroup) in your 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;

    customViewGroup 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;
    }
}

并在活动的onCreate方法中调用preventStatusBarExpansion方法.完成!

And call the preventStatusBarExpansion method in the onCreate method of the activity. Done!

选项2:首先,将以下权限添加到Androidmanifest.xml文件:

OPTION 2: First, add the following permission to the Androidmanifest.xml file:

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

第二,在您的活动中定义以下类范围变量:

Second, define the following class-scope variables in your activity:

// To keep track of activity's window focus
boolean currentFocus;

// To keep track of activity's foreground/background status
boolean isPaused;

Handler collapseNotificationHandler;

第三,重写onWindowFocusChanged(boolean):

Third, override onWindowFocusChanged(boolean):

@Override
public void onWindowFocusChanged(boolean hasFocus) {

    currentFocus = hasFocus;

    if (!hasFocus) {

        // Method that handles loss of window focus
        collapseNow();
    }
}

首先,定义collapseNow();方法:

Forth, define the collapseNow(); method:

public void collapseNow() {

    // Initialize 'collapseNotificationHandler'
    if (collapseNotificationHandler == null) {
        collapseNotificationHandler = new Handler();
    }

    // If window focus has been lost && activity is not in a paused state
    // Its a valid check because showing of notification panel
    // steals the focus from current activity's window, but does not 
    // 'pause' the activity
    if (!currentFocus && !isPaused) {

        // Post a Runnable with some delay - currently set to 300 ms
        collapseNotificationHandler.postDelayed(new Runnable() {

            @Override
            public void run() {

                // Use reflection to trigger a method from 'StatusBarManager'                

                Object statusBarService = getSystemService("statusbar");
                Class<?> statusBarManager = null;

                try {
                    statusBarManager = Class.forName("android.app.StatusBarManager");
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }

                Method collapseStatusBar = null;

                try {

                    // Prior to API 17, the method to call is 'collapse()'
                    // API 17 onwards, the method to call is `collapsePanels()`

                    if (Build.VERSION.SDK_INT > 16) {
                        collapseStatusBar = statusBarManager .getMethod("collapsePanels");
                    } else {
                        collapseStatusBar = statusBarManager .getMethod("collapse");
                    }
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }

                collapseStatusBar.setAccessible(true);

                try {
                    collapseStatusBar.invoke(statusBarService);
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }

                // Check if the window focus has been returned
                // If it hasn't been returned, post this Runnable again
                // Currently, the delay is 100 ms. You can change this
                // value to suit your needs.
                if (!currentFocus && !isPaused) {
                    collapseNotificationHandler.postDelayed(this, 100L);
                }

            }
        }, 300L);
    }   
}

最后,重写onPause()和onResume方法:

Finally, override onPause() and onResume methods:

@Override
protected void onPause() {
    super.onPause();

    // Activity's been paused      
    isPaused = true;
}

@Override
protected void onResume() {
    super.onResume();

    // Activity's been resumed
    isPaused = false;
}

完成!

注意:将应用置于信息亭模式时,我使用了第一个选项,该模式根本不显示通知面板. 另一方面,第二个选项效果很好,但是它允许通知面板显示一小段时间,并且用户可以快速单击通知面板顶部的设置图标以退出信息亭模式.

NOTE: I used the 1st option when putting an app in kiosk mode, which doesn't show the notification panel at all. On the other hand, the 2nd option works well, but it allows the notification panel to show up for a short time, and user can quickly click the settings icon on the top of the notification panel to quit the kiosk mode.

希望这会对您有所帮助. 干杯!

Hope this will help you. Cheers!

这篇关于如何在Android中禁用状态栏的单击和下拉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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