gpt4 book ai didi

Android 在 oreo 中隐藏和禁用通知(状态)栏

转载 作者:行者123 更新时间:2023-11-29 18:36:41 24 4
gpt4 key购买 nike

通过禁用拉动和单击状态栏来 kiosking 应用程序的方法在 android 8 上不起作用。如在 How to disable status bar click and pull down in Android? 上回答的那样.

我已经在 android 7 及更低版本上测试过它并且它可以工作,但是在 android 8 上拉动时状态栏仍然拉下。

我没有找到任何解决方案。如果有任何解决方案也适用于奥利奥,请告诉我。

谢谢!

最佳答案

我在 MainActivity 中使用以下代码。

//Global Declaration
Handler collapseNotificationHandler;

@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.d(tag, "window focus changed");

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
collapseNow();
}
}

折叠方法

public void collapseNow() {

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

// 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();
}
// Currently, the delay is 10 ms. You can change this
// value to suit your needs.
collapseNotificationHandler.postDelayed(this, 10L);
}
}, 10L);
} catch (Exception e) {
e.printStackTrace();
}
}

关于Android 在 oreo 中隐藏和禁用通知(状态)栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54068931/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com