I'm making a game and if the activity is left in any way by the user (back or home key pressed), the activity needs to end the game by posting to a script and ending the activity.
I can detect if the back key is pressed, however, I cannot find any valid method to detect if the home key is pressed. I can't just end the game in the Activity_Pause method because let's say the user receives a phone call mid-game.
I understand you can't trap the event, however, has anyone found a way to see if the activity was left by the user instead of something else like a phone call sending it to the background.
Ok here is the work around if you insist. Android next version may just close the loophole.
boolean mKeyPress;
boolean mUserLeaveHint;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
mKeyPress = true;
return super.onKeyDown(keyCode, event);
@Override
protected void onUserLeaveHint()
super.onUserLeaveHint();
mUserLeaveHint = true;
@Override
protected void onPause()
super.onPause();
if (!mKeyPress && mUserLeaveHint)
// HOME_KEY is pressed
Looks like a duplicate of this one
Android, How to receive home button click through broadcast receiver?
@Override
public boolean dispatchKeyEvent(KeyEvent keyevent) {
if (keyevent.getKeyCode() == KeyEvent.KEYCODE_HOME) {
//Do here what you want
return true;
return super.dispatchKeyEvent(event);
来源:https://stackoverflow.com/questions/15374673/android-see-if-home-key-is-pressed
问题I'm making a game and if the activity is left in any way by the user (back or home key pressed), the activity needs to end the game by posting to a script and ending the activity.I can detect if the...
前言通过上一篇文章(Binder之应用层总结与分析)可以了解到进程间通讯的一个大致情况,像今天要提到的Activity启动过程,也是以Binder为通讯方式。系统对这个工作过程做了很多封装,使得启动一个Activity变得十分简单。这看似简单的背后,实际上是Activity与ActivityManagerService之间多次通讯的结果。
阅读该篇文章建议配合源码一起食用,味道更佳。Launche
private void imitatePress
Home
(Context context)
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setF
1.在终端输入appium_doctor,返回信息中有
ANDROID
_
HOME
is not set
2.然后去‘环境变量配置’处将所有的变量名为‘
ANDROID
_SDK_
HOME
’改为“
ANDROID
_
HOME
”
3.关闭终端,重新运行appium_doctor,起码不会报
ANDROID
_
HOME
is not set了····
override fun onUserLeaveHint() {
super.onUserLeaveHint()
Log.e("
HOME
listener :","
HOME
时间 = "+getCurrentTime())
private fun getCurrentTime() : String {
var calendar: Calendar = Calendar.getInstance()
创建
home
receiverimport
android
.content.BroadcastReceiver;
import
android
.content.Context;
import
android
.content.Intent;
import
android
.content.IntentFilter;
public class
Home
Key
EventBroadCastReceiver ...
工作中有一个场景是只要返回桌面,重新回到A页面时会有个锁屏页面B,必须输入密码才可以进入A页面,现在是返回桌面的处理问题,首要想到监听
home
键,再判定当前最顶层页面是不是桌面即可
1,onUserLeaveHint方法,但是有个问题就是跳转其他app也会触发,因此不行
//在基类BaseActivity重写方法监听
@Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
有时候我们在开发的时候需要在应用中获取Power
Key
,
Home
Key
等按键事件,但是我们知道这些按键属于特殊的按键,应用中并不能获取到。
这些按键在framework层已经被处理掉了,因此我们需要通过一些方法来让应用能获取到。
对于
Home
Key
, Power
key
这些特殊的按键,应用是不应该获取到的,因为这些按键需要处理一些特殊的事情。
应用获取到后可能会造成
这是一个 Python 代码片段,用于检查按钮是否被按下。前提是已经定义了一个名为 `button` 的按钮对象,并且该对象具有 `ispre
sse
d` 属性。如果该属性值为 `True`,则表示按钮已被按下;否则,按钮未被按下。以下是一个示例代码片段:
```python
if button.ispre
sse
d:
print("Button is pre
sse
d!")
else:
print("Button is not pre
sse
d.")