【发布时间】:2016-02-12 18:12:09
【问题描述】:
我正在开发一个锁屏应用程序,到目前为止,我已经完成了应用程序运行所需的一切。
但我无法禁用 Android 设备中作为虚拟和软可用的主页/菜单按钮。我在 SO 和其他网站上浏览了所有可能的答案,但无法实现。
是否有任何经过测试和可行的解决方法? 提前致谢。
【问题讨论】:
标签: android android-homebutton
我正在开发一个锁屏应用程序,到目前为止,我已经完成了应用程序运行所需的一切。
但我无法禁用 Android 设备中作为虚拟和软可用的主页/菜单按钮。我在 SO 和其他网站上浏览了所有可能的答案,但无法实现。
是否有任何经过测试和可行的解决方法? 提前致谢。
【问题讨论】:
标签: android android-homebutton
一种方法是显示一个对话框,其中LayoutParams 类型设置为TYPE_SYSTEM_ERROR,并将此对话框的所有者设置为您的“锁屏”活动以阻止主页按钮。
这是一个如何做到这一点的示例: 更新: 看起来这仅适用于 Android 4.+ 之前的版本 https://github.com/Joisar/LockScreenApp/blob/master/LockScreenApp/src/com/mehuljoisar/lockscreen/utils/LockscreenUtils.java
另一种方法是将 contentView 直接添加到 LayoutParams 类型设置为 TYPE_SYSTEM_ERROR 的 WindowManager
...
onCreate(){
//setContentView(R.layout.main_content);
//instead add a View directly to the WindowManager
View contentView = View.inflate(this, R.layout.main_content, null);
LayoutParams lockLayoutParams = new LayoutParams();
lockLayoutParams.width = LayoutParams.MATCH_PARENT;
lockLayoutParams.height = LayoutParams.MATCH_PARENT;
lockLayoutParams.type = LayoutParams.TYPE_SYSTEM_ERROR;
//LOCK
getWindowManager().addView(contentView, lockLayoutParams);
...
//UNLOCK
getWindowManager().removeView(contentView);
我使用这种方法的缺点是它看起来不支持更复杂的视图。我在 Fragments 等中的 ListViews 有很多闪烁。
一些使用它的示例项目:
更多类似问题的答案在这里: How to disable Home and other system buttons in Android?
【讨论】: