android - 禁用状态栏拉入奥利奥
问题描述
通过禁用状态栏的拉动和单击来显示应用程序的方法在 android 8 上不起作用。正如关于 如何在 Android 中禁用状态栏单击和下拉?
- 您可以在状态栏上放置一个窗口以禁用任何触摸或下拉。
如this answer所述,这种方法确实适用于android 7及更低版本,但是这种方法不适用于android 8(oreo)。
我已经在 android 7 和更少版本上对其进行了测试,它可以工作,但是在 android 8 上拉时状态栏仍然拉下。
如果您对此有解决方案,请提供帮助。
谢谢你们。
解决方案
对于 8 及以上版本,您无法真正将视图完全覆盖在其他应用程序之上,因此您必须做的是,当您将抽屉拉下时,将抽屉恢复得如此之快,以至于用户将无法点击它上面的任何内容。这是执行此操作的代码。确保您在活动中执行此操作。
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (!hasFocus) {
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
// Method that handles loss of window focus
new BlockStatusBar(this,false).collapseNow();
}
}
}
然后,隐藏状态栏的助手类如下所示。
public class BlockStatusBar {
Context context;
// To keep track of activity's window focus
boolean currentFocus;
// To keep track of activity's foreground/background status
boolean isPaused;
public static Handler collapseNotificationHandler;
Method collapseStatusBar = null;
public BlockStatusBar(Context context,boolean isPaused) {
this.context=context;
this.isPaused=isPaused;
collapseNow();
}
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 = context.getSystemService("statusbar");
Class<?> statusBarManager = null;
try {
statusBarManager = Class.forName("android.app.StatusBarManager");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
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);
}
if (!currentFocus && isPaused) {
collapseNotificationHandler.removeCallbacksAndMessages(null);
}
}
}, 1L);
}
}
}
推荐阅读
- javascript - 放置在工具栏中时,我的输入字段没有被捕获
- javascript - 如何使用 Flow 在 JavaScript 中定义 Hex 类型?
- excel - 使用 Microsoft Graph API 从 Sharepoint 站点中提取 Excel.xlsx 工作簿
- java - Java Stream 分组和计数出现
- python - 使用由 Augmentor 生成的图像训练 keras 模型
- python - 隐藏生成的边界框的准确率百分比
- excel - 如何使我的图表数据库中的范围动态
- typescript - 如何通过子 ID 过滤父对象?
- c - C警告:赋值从没有强制转换/指向整数数组的指针的指针中生成整数
- android - Android Retrofit 动态查询变量