如何在android中使状态栏不可触摸?

时间:2014-04-07 06:26:55

标签: android android-layout view statusbar

我正在尝试为Android设置锁定屏幕,版本大于蜂窝。我一个月以来一直在寻找一种有效的解决方案但是没有成功。我将具体说明我的情况。

  • 在特定活动中,我希望状态栏可见但无法触及全部。换句话说,用户不应该将其拉下来。
  • 当我的活动失去焦点时,我真的不想要解决状态栏的解决方案。这不起作用!在我的Galaxy Note-2上尝试一两次之后,我仍然能够拉下我的状态栏。
  • 我不想将我的活动置于全屏模式。这将隐藏状态栏,但我希望它可见。
  • 我知道有可能。有线索说不可能这样做。但我知道这样做是可能的,因为还有其他自定义锁定屏幕可以实现相同的功能。 (例如,Dodol储物柜,储物柜+等)

我真的很感激工作代码。

提前致谢。

1 个答案:

答案 0 :(得分:1)

让我们尝试一下它不会让状态栏完全不可触及,但它会阻止它被拖动,首先在mainfest文件中为EXPAND_STATUS_BAR提供权限

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

在您的活动中声明这些变量:

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

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

Handler collapseNotificationHandler;

它会要求您覆盖以下方法,覆盖它

@Override
public void onWindowFocusChanged(boolean hasFocus) {

    currentFocus = hasFocus;

    if (!hasFocus) {

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

定义collapse_now方法

public void collapse_now() {

    // Initialization for '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);
    }   
}

处理活动的暂停状态

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

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

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

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