0

我想在我的应用程序中用户单击的主页按钮上编写一些代码。

我写了以下代码:

 @Override
        public void onAttachedToWindow() {
            super.onAttachedToWindow();
            this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
        }

     @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {     

            if(keyCode == KeyEvent.KEYCODE_HOME)
            {
               //The Code Want to Perform. 
                Toast.makeText(getApplicationContext(), flag+"In Here", Toast.LENGTH_SHORT).show();

            }
            return true;
        }

此代码给了我 Toast 消息,但不会最小化我的应用程序。

一旦我删除以下代码:

  @Override
            public void onAttachedToWindow() {
                super.onAttachedToWindow();
                this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
            }

并且只保留:

 @Override
            public boolean onKeyDown(int keyCode, KeyEvent event) {     

                if(keyCode == KeyEvent.KEYCODE_HOME)
                {
                   //The Code Want to Perform. 
                    Toast.makeText(getApplicationContext(), flag+"In Here", Toast.LENGTH_SHORT).show();

                }
                return true;
            }

它没有向我显示敬酒消息。

请帮我。

我也试过:

返回假

暂停方法

但没有奏效。

4

3 回答 3

2

Try return false instead so you don't catch the click event but sends it along to the OS as well.

于 2013-08-28T12:21:10.400 回答
2

You should have this code,

 @Override
            public void onAttachedToWindow() {
                super.onAttachedToWindow();
                this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
            }

in your code. Removing this will not trigger the Home key action.

Now to minimize the app, do the following,

    @Override
                public boolean onKeyDown(int keyCode, KeyEvent event) {     

                    if(keyCode == KeyEvent.KEYCODE_HOME)
                    {
                       //The Code Want to Perform. 
                        Toast.makeText(getApplicationContext(), flag+"In Here", Toast.LENGTH_SHORT).show();
                 Intent gotoHome= new Intent(Intent.ACTION_MAIN);
                 gotoHome.addCategory(Intent.CATEGORY_HOME);
                 gotoHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                 startActivity(gotoHome);
                    }
                    return true;
                }

But I think this solution no more works for higher API levels.

于 2013-08-28T12:21:44.460 回答
2

在 onKeyDown 中返回 true 表示事件已被处理。

修改您的代码,return false以便操作系统进一步处理事件。

例如:

        public boolean onKeyDown(int keyCode, KeyEvent event) {     

            if(keyCode == KeyEvent.KEYCODE_HOME)
            {
               //The Code Want to Perform. 
                Toast.makeText(getApplicationContext(), flag+"In Here", Toast.LENGTH_SHORT).show();

            }
            return false; // Signals the KeyEvent.KEYCODE_HOME to be processed further.
        }

请参阅onKeyDown文档

于 2013-08-28T12:23:40.777 回答