Android 暂停后退键和菜单键上的活动

发布于 2024-11-26 17:21:44 字数 601 浏览 0 评论 0原文

在我的应用程序中,我只有一项活动,并且我使用 ViewAnimator 来更改屏幕。对于菜单和后退键,我尝试暂停活动(就像主页键一样)并显示菜单或返回到上一个View

愤怒的小鸟也有同样的想法(愤怒的小鸟里约热内卢做了与我想要的完全相同的事情)。

我还检查了 OnKeyDown 事件以了解 Home 键如何工作,但我认为它是由系统处理的。

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/app/Activity.java#Activity.onKeyDown%28int %2Candroid.view.KeyEvent%29

谢谢。

In my application I have only one activity and I am using ViewAnimator for changing the screen. For the Menu and Back Keys I am trying to pause the activity (like home key does) and show a menu or go back to previous View.

Angry Birds do the same think (Angry Birds Rio do exactly the same thing what I want).

I have also checked the OnKeyDown event to find out how the Home key work but I think it is handled by the system.

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/app/Activity.java#Activity.onKeyDown%28int%2Candroid.view.KeyEvent%29

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泅渡 2024-12-03 17:21:44

为了捕获菜单键:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //return true to show some menu
    //return false to not show any menu
}

请在此处阅读更多信息: http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu(android.view.Menu)

为了捕获后退键事件:

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        //do your stuff and Return true to prevent this event from being propagated further
        return true;
    }

    return false;
}

在此处阅读更多信息:http://developer.android.com/reference/android/app/Activity .html#onKeyUp(int, android.view.KeyEvent)

编辑:
暂停应用程序的逻辑取决于您。但是您可以显示一个 AlertDialog ,最多包含三个按钮和/或任何您想要的视图:

In order to catch the menu key:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //return true to show some menu
    //return false to not show any menu
}

Read more here: http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu(android.view.Menu)

Ir order to catch the back key event:

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        //do your stuff and Return true to prevent this event from being propagated further
        return true;
    }

    return false;
}

Read more here: http://developer.android.com/reference/android/app/Activity.html#onKeyUp(int, android.view.KeyEvent)

EDIT:
Pausing your application's logic is up to you. But you can show an AlertDialog with up to three buttons and/or any View you want: http://developer.android.com/reference/android/app/AlertDialog.html

荒人说梦 2024-12-03 17:21:44

Home 键永远不会直接被应用程序事件处理程序捕获,有一种方法可以通过一些工作来检测它是否发生,请参阅 onUserLeaveHint 获取主要思想,然后使用 onUserInteraction 尝试确定它是否是 home 键。

不确定您的意图是什么,我有点不清楚这是否就是您想要的答案。

无论如何,根据您的应用程序,您可能需要将其与其他一些状态管理结合起来,但对于非常简单的应用程序,它在大多数情况下都有效。我只用它来进行分析,但它可能对你有用,我会仔细研究一下,以防万一

long userInteractionTime = 0;

@Override
public void onUserInteraction() {
    userInteractionTime = System.currentTimeMillis();
    super.onUserInteraction();
}

@Override
public void onUserLeaveHint() {
    long uiDelta = (System.currentTimeMillis() - userInteractionTime);

    super.onUserLeaveHint();
    Log.i("bThere","Last User Interaction = "+uiLag);
    if (uiDelta < 100)
        Log.i("appname","Home Key Pressed");    
    else
        Log.i("appname","Leaving activity for some other reason!");  
}

The home key will never be trapped by an applications event handlers directly, there is way to detect that it happened with a little work see onUserLeaveHint for the main idea and then onUserInteraction to try and determine if it's the home key.

Not sure exactly what your intent is and I am a little unclear if that's all you want answered.

Anyway dependent on your application you might need to combine this with some other state management, but for a very simple applications it works most of the time. I only use it for analytic's but it might work for you, I'll just through it out in case it does

long userInteractionTime = 0;

@Override
public void onUserInteraction() {
    userInteractionTime = System.currentTimeMillis();
    super.onUserInteraction();
}

@Override
public void onUserLeaveHint() {
    long uiDelta = (System.currentTimeMillis() - userInteractionTime);

    super.onUserLeaveHint();
    Log.i("bThere","Last User Interaction = "+uiLag);
    if (uiDelta < 100)
        Log.i("appname","Home Key Pressed");    
    else
        Log.i("appname","Leaving activity for some other reason!");  
}
偏爱自由 2024-12-03 17:21:44

当我正确地回答你的问题时,你不想改变主页按钮的行为,对吧?

要处理 back 和 menuKey,您可以使用 onKeyDown 回调,如下所示:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_BACK){
        //DO STUFF
        return true;
    }
            //else if(keyCode == KeyEvent.KEYCODE_MENU){
            //       //DO OTHER STUFF
            //       return false; // I think it's better to use the onMenuOpenCallback
            //       for that...
            //}

    return super.onKeyDown(keyCode, event);
}

对于菜单按钮,您也可以使用 onKeyDown 回调,但我认为这不是最好的方法,因为您无论如何都想显示菜单。
您可以使用 onMenuOpend 回调来代替(尽管您应该在准确调用此方法时检查文档):

    @Override
public boolean onMenuOpened(int featureId, Menu menu) {
          //DO STUFF
          return true;
    }

希望这会有所帮助...
摩根大通

When I get your question correctly you don't want to change the behavior of the home button, right?

To handle the back and menuKey you can use the onKeyDown callback like this:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_BACK){
        //DO STUFF
        return true;
    }
            //else if(keyCode == KeyEvent.KEYCODE_MENU){
            //       //DO OTHER STUFF
            //       return false; // I think it's better to use the onMenuOpenCallback
            //       for that...
            //}

    return super.onKeyDown(keyCode, event);
}

For the menu button you could also use the onKeyDown callback but I think it's not the best way, since you want to show a menu anyway.
You can use the onMenuOpend callback for that instead (although you should check the docs when this method is called exactly):

    @Override
public boolean onMenuOpened(int featureId, Menu menu) {
          //DO STUFF
          return true;
    }

Hope this helps...
jpm

一场信仰旅途 2024-12-03 17:21:44

如果应用程序冻结或发生其他情况,用户想要退出应用程序,则无法覆盖主页键。这将是他们离开的唯一途径。但是主页键会暂停活动,例如在动画中间按主页键并再次切换到应用程序,动画将在您按主页键的位置继续。

you can't override the home key in case the user wants to exit the app if it freezes or something. it would be the only way for them to leave. But home key pauses the activity e.g. in the middle of the an animation press home key and switch to app again the animation continue where you pressed the home key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文