如何检测用户pressing家在我的活动重点? [英] How can I detect user pressing HOME key in my activity?

查看:258
本文介绍了如何检测用户pressing家在我的活动重点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能告诉我怎么能我的活动检测用户pressing <大骨节病>首页键?

Can you please tell me how can my activity detect user pressing HOME key?

感谢你。

推荐答案

更新 [2015年八月]:由于每的 JM主,根据需要为设备棒棒堂及以上这个答案可能无法正常工作。请参阅<一href="http://stackoverflow.com/questions/26400469/alternative-to-getrunningtasks-in-android-l">Alternative在Android的大号 getRunningTasks

Update [August 2015]: As per the comment of JM Lord, this answer might not work as desired for devices Lollipop and above. See Alternative to getRunningTasks in Android L

虽然,它的一个老问题,但大部分的答案并没有为我在API级别17以上的工作。我回答什么终于为我工作。我尝试过很多方法,但他们没有工作今天。我试了

Although, its an old question but most of the answers didn't work for me in API level 17 and above. I am answering what finally worked for me. I've tried many method but none of them are working as today. I tried answers on

<一个href="http://stackoverflow.com/questions/4783960/call-method-when-home-button-$p$pssed-on-android">Call方法时,home键pressed在Android ,

检测home键preSS的机器人

<一个href="http://stackoverflow.com/questions/2079691/overriding-the-home-button-how-do-i-get-rid-of-the-choice">Overriding Home键 - 我该如何摆脱的选择?要点是,家庭就像是一个紧急逃生按钮。所以你不能忽略它,但可以检测到它在某些方面。

Overriding the Home button - how do I get rid of the choice?. The gist was that Home is like an emergency escape button. So you can't override it but can detect it in some ways.

部分的那些我试过(包括上面的答案)和没有工作是:

Some of the ones I tried( including above answers) and didn't work are:

  1. 使用 键code == KeyEvent.KEY code_HOME 在许多方面为 如上所述。现在,如果你阅读的文档 KeyEvent.KEY code_HOME ,它说此密钥是由处理 框架是永远不会传递给应用程序。因此,它没有更多的 现在有效。

  1. Using keyCode==KeyEvent.KEYCODE_HOME in many ways as stated above. Now, if you read the documentation of KeyEvent.KEYCODE_HOME, it says that This key is handled by the framework and is never delivered to applications. So its no more valid now.

我试图用 onUserLeaveHint() 。该文件说:

I tried using onUserLeaveHint(). The documentation says:

古称活动周期的一部分,当活动即将   进入背景作为用户选择的结果。例如,   当用户presses Home键,onUserLeaveHint()会   叫,但是当一个电话呼叫到来使在呼叫的活动   将自动被推上前台。

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.

与的问题是,当你开始一个活动的方法也被调用 从正在调用 onUserleaveLint()的活动中, 因为是我的情况。请参阅<一href="http://stackoverflow.com/questions/6612058/android-onback$p$pssed-onuserleavehint/8749061#8749061">Android更多的onBack pressed / onUserLeaveHint 的问题。因此,它不知道, 那也只能由pressing home键调用。

The issue with that is that the method also gets called when you start an Activity from within the activity where you are calling onUserleaveLint(), as was my case. See Android onBackPressed/onUserLeaveHint question for more. So its not sure that it would be call ONLY by pressing home button.

调用的onStop()。它也可以当一个活动来对现有的活动的顶部和称为完全覆盖它。因此,这也不会工作。

Calling onStop(). It can also be called when an activity comes on top of the existing activity and fully covers it. So this also wont work.

最后的下面的工作对我来说:

看<一href="http://stackoverflow.com/questions/3278895/how-to-check-current-running-applications-in-android">How在Android中,检查当前正在运行的应用程序?,你可以说,如果你是显示在较长pressing home键的近期任务,然后将其发送到后台。

Seeing How to check current running applications in Android? you can say that if yours is the recent task that is shown on long pressing the home button, then it was send to background.

所以,在你的的onPause()你在哪里试图检测home键pressed的活动,您可以检查应用程序是否已发送到背景。

So, In your onPause() of the activity where you are trying to detect the home button pressed, you can check whether the application has been sent to background.

@Override
public void onPause() {
    if (isApplicationSentToBackground(this)){
        // Do what you want to do on detecting Home Key being Pressed 
    }
    super.onPause();
}

函数来检查是否你的是已经最近发送到后台应用程序:

Function to check whether yours is the app which has been most recently sent to the background:

public boolean isApplicationSentToBackground(final Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }
    return false;
}

感谢@idish的指出,不要忘记在您的清单以下权限:

Thanks to @idish for pointing out that don't forget to include the following permission in your manifest:

<uses-permission android:name="android.permission.GET_TASKS" />

我不知道是否有这个缺点任何,但它的工作对我来说很好。希望它可以帮助别人一天。

I am not sure whether there are any downsides of this, but it worked for me well. Hope it helps someone someday.

PS:如果你已发起了 FLAG_ACTIVITY_NO_HISTORY 标记的活动使用此方法,那么这将不会因为它会检查最近的历史搞清楚是有益的如果首页按钮被点击。

P.S: If you use this method in an Activity which has been launched with FLAG_ACTIVITY_NO_HISTORY flag, then this won't be useful as it checks the recent history for figuring out if Home button was clicked.

这篇关于如何检测用户pressing家在我的活动重点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆