Android - 是否可以禁用单击主页按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2162182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - Is It possible to disable the click of home button
提问by Vinayak Bevinakatti
I have an application, when it launches I have to disable all the buttons on Android device, I succeeded in disabling end call and others. I need to disable home button click. It should not produce any action on click.
我有一个应用程序,当它启动时,我必须禁用 Android 设备上的所有按钮,我成功地禁用了结束呼叫和其他按钮。我需要禁用主页按钮点击。它不应该在点击时产生任何动作。
Any suggestions highly appreciated
任何建议高度赞赏
回答by synic
I'm pretty sure Toddler Lock just uses a BroadcastReciever and listens for Intent.ACTION_MAIN
and the category Intent.CATEGORY_HOME
- that's why when you first launch it, it tells you to check the "use this application as default" box, and makes you select toddler lock.
我很确定 Toddler Lock 只使用 BroadcastReciever 并侦听Intent.ACTION_MAIN
类别Intent.CATEGORY_HOME
- 这就是为什么当您第一次启动它时,它会告诉您选中“将此应用程序用作默认值”框,并让您选择 toddler lock。
So, it's not really blocking the Home button at all, it's just setting itself up as the default broadcast receiver for:
所以,它根本没有真正阻止 Home 按钮,它只是将自己设置为默认广播接收器:
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
When you launch Toddler Lock, it probably sets an internal flag, and if you press the home button, it just brings the window to the front. If the flag is not set, it probably launches Launcher explicitly.
当您启动 Toddler Lock 时,它可能会设置一个内部标志,如果您按下主页按钮,它只会将窗口带到前面。如果未设置该标志,它可能会显式启动 Launcher。
I hope that makes sense. It's just a theory, but I'm almost 100% sure that's how it's done.
我希望这是有道理的。这只是一个理论,但我几乎 100% 确定它是如何完成的。
回答by Jeffrey
Add following code to your activity:
将以下代码添加到您的活动中:
@override
public void onAttachedToWindow()
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
Edit:
编辑:
This works in all older version of android. But will not workin ICS
and jelly bean and will give you crash in app
这适用于所有旧版本的android。但不会在ICS
和果冻豆中工作,并且会让你在应用程序中崩溃
What does this 4 line java code means in android application?
回答by Jean-Fran?ois
Add this in your manifest.xml for your main activity:
在您的 manifest.xml 中为您的主要活动添加以下内容:
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
The HOME button will always (re-)launch your activity. Works in Froyo.
主页按钮将始终(重新)启动您的活动。在弗罗约工作。
回答by amiekuser
I found a way to tackle HOME key. For your application set the manifest as
我找到了解决 HOME 键的方法。对于您的应用程序,将清单设置为
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY"/>
Now ur application is an alternate Launcher application.
现在你的应用程序是一个备用的启动器应用程序。
Use the adb, and disable the launcher application using package manager
使用 adb,并使用包管理器禁用启动器应用程序
pm disable com.android.launcher2
Now the Home key press will laways stay in the same screen.
现在 Home 键按下将一直停留在同一屏幕上。
回答by davide.gironi
回答by Richard Rout
A further addition to Jeffreys post, here is something that worked for me (and still allows translucent theme)
Jeffreys 帖子的进一步补充,这里有一些对我有用的东西(并且仍然允许半透明主题)
@Override
public void onAttachedToWindow()
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
super.onAttachedToWindow();
}
Becuase it makes the keyguard come up, you could also just disable the keyguard whilst the app is in use:
因为它使键盘保护器出现,您也可以在应用程序正在使用时禁用键盘保护器:
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
This works really well for making your own keyguard app.
这对于制作您自己的键盘锁应用程序非常有效。