Android 覆盖主页按钮 - 我如何摆脱选择?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2079691/
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
Overriding the Home button - how do I get rid of the choice?
提问by Ted
When creating one Intent so that MyActivity reacts to a User pressing the Home-button is easy using the XML markup:
创建一个 Intent 以便 MyActivity 对用户按下主页按钮做出反应时,使用 XML 标记很容易:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
I want to know how to avoid getting the choice of "what activity do you want to use" for the Home screen? HTC has made its "Touch Flo" (Sense) override the default "start" Activity and I never get the question if I want to use "Start" or "TouchFlo" usually. However, when I added my own Activity I always get the question.
我想知道如何避免在主屏幕上选择“您想使用什么活动”?HTC 已使其“Touch Flo”(Sense)覆盖默认的“开始”活动,如果我通常想使用“开始”或“TouchFlo”,我永远不会得到问题。但是,当我添加自己的 Activity 时,我总是会遇到问题。
Yes, I know that I can check the "Use this as standard"-checkbox, but that's not what I want right now. So, question is: can I make the system override everything else and always use MyActivity as default?
是的,我知道我可以选中“将此用作标准”复选框,但这不是我现在想要的。所以,问题是:我可以让系统覆盖其他所有内容并始终使用 MyActivity 作为默认值吗?
Next, I really only want to override the normal Home Screen when my app is running. If its not running, everything should work as normal, ie MyActivity should NOT be associated with the Home button.
接下来,我真的只想在我的应用程序运行时覆盖正常的主屏幕。如果它没有运行,一切都应该正常工作,即 MyActivity 不应与主页按钮相关联。
回答by Dave Webb
You can't permanently override the Home button without the user confirming it.
未经用户确认,您无法永久覆盖主页按钮。
One argument for why this is the case is a security one. The Home button is the one way a user can be guaranteed to exit any application. If you could make the Home button launch your application instead of the Home screen without the user confirming this change, it would then be very easy to write a malicious application that hiHymaned a user's phone.
为什么会出现这种情况的一个论据是安全论据。主页按钮是保证用户退出任何应用程序的一种方式。如果您可以让主页按钮启动您的应用程序而不是主屏幕,而无需用户确认此更改,那么编写一个劫持用户手机的恶意应用程序就很容易了。
Alternatively, your application could contain a replicate Home Screen that harvested a user's Google account details; not that hard since the source is available. If your application could silently replace the default Home Screen, it would be hard for the user to tell this had happened.
或者,您的应用程序可以包含一个复制的主屏幕,用于收集用户的 Google 帐户详细信息;没有那么难,因为源可用。如果您的应用程序可以默默地替换默认的主屏幕,用户将很难知道这已经发生了。
Also, do you really want to override Home? Home is like an emergency escape button. Pressing Home twice will always take a user back to the center panel of the Home Screen, so whatever application they're running, it's easy for a user to get back to somewhere they know. You shouldn't really be overriding this unless you're producing a full Home replacement.
另外,您真的要覆盖 Home 吗?家就像一个紧急逃生按钮。按两次 Home 将始终将用户带回到主屏幕的中央面板,因此无论他们正在运行什么应用程序,用户都可以轻松返回到他们知道的地方。除非您要生产完整的 Home 替代品,否则您真的不应该覆盖它。
回答by lucasneo
@Override
public void onAttachedToWindow()
{
Log.i("TESTE", "onAttachedToWindow");
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
With this method, the HOME Button stops working in this activity (only this activity). Then you just reimplement as it was a normal button event (the back button for instance).
使用此方法,HOME 按钮在此活动中停止工作(仅此活动)。然后你只需重新实现它,因为它是一个普通的按钮事件(例如后退按钮)。
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
Log.i("TESTE", "BOTAO HOME");
return true;
}
return super.onKeyDown(keyCode, event);
}
回答by Totumus Maximus
The overridable onUserLeaveHint()
lifecycle hook can be used when the user presses the home button.
onUserLeaveHint()
当用户按下主页按钮时,可以使用可覆盖的生命周期钩子。
This hook has been present since API level 3.
这个钩子从 API 级别 3 开始就存在了。
@Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
// Your code here
}
More info about the method:
有关该方法的更多信息:
Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice.
For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground,
onUserLeaveHint()
will not be called on the activity being interrupted.In cases when it is invoked, this method is called right before the activity's
onPause()
callback.
当 Activity 由于用户选择而即将进入后台时,作为 Activity 生命周期的一部分调用。
例如,当用户按下 Home 键时,onUserLeaveHint() 会被调用,但是当有来电导致通话中的 Activity 被自动带到前台时,
onUserLeaveHint()
不会在被中断的 Activity 上调用。在调用它的情况下,此方法在活动的
onPause()
回调之前被调用。
Source: https://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint()
来源:https: //developer.android.com/reference/android/app/Activity.html#onUserLeaveHint()
回答by mylock
Well, the user would still have to choose the regular home as their home with use as default checked to stop the prompt from coming back. However, I believe it is possible for you to then modify the system settings in some way to incidentally have your particular activity be considered the default home, such that press of home would then do nothing or appear locked to the user at the main activity, and I understand you wish to launch your user into other activities from there, giving them a fast home press return to this root Launcher. I can see the benefit of this completely, and it may even benefit what I'm developing if I choose to implement multiple widget screens that the user can flip left or right between.
好吧,用户仍然必须选择常规家作为他们的家,并选中默认使用以阻止提示返回。但是,我相信您可以以某种方式修改系统设置,以便将您的特定活动视为默认主页,这样按下主页就不会执行任何操作或在主要活动中对用户显示锁定,我知道您希望从那里启动您的用户进入其他活动,让他们快速返回到这个根启动器。我可以完全看到这样做的好处,如果我选择实现用户可以左右翻转的多个小部件屏幕,它甚至可能有益于我正在开发的内容。
There is at least 1 app out there that I've downloaded which appears to be doing exactly this. When the user exits the app, it restores the user's regular default home preference. The app instructs the users very explicitly to choose Launcher as their default home and never do that with the app itself. It has a few different "exit" methods which give the user the choice of which home to return to if they have multiple, and one that just exits to their regular default.
我已经下载了至少 1 个应用程序,它似乎就是这样做的。当用户退出应用程序时,它会恢复用户的常规默认主页首选项。该应用程序非常明确地指示用户选择启动器作为他们的默认主页,并且永远不要对应用程序本身这样做。它有几种不同的“退出”方法,如果用户有多个,它们可以让用户选择返回哪个家,而一个只是退出到他们的常规默认值。
I am researching this as well and will report back with progress & source.
我也在研究这个,并将报告进度和来源。
回答by Minto
Use onUserLeaveHint().
使用 onUserLeaveHint()。
onUserLeaveHint() is a protected method as other lifecycle methods of the activity and if you are handling onUserLeaveHint this will take care of the following case
onUserLeaveHint() 是一个受保护的方法作为活动的其他生命周期方法,如果您正在处理 onUserLeaveHint,这将处理以下情况
- When User hits home key
- When User hits back key
- When User hits annunciator bar
- 当用户点击主页键时
- 当用户回击键时
- 当用户点击提示栏时
Here you can catch Homeb button click.
在这里你可以捕捉到 Homeb 按钮的点击。