How to capture app switch key using onKeyDown in Android?

前端 未结 3 489
情书的邮戳
情书的邮戳 2020-12-07 04:16

I am trying to capture app switch key and home key on android 3.1 and 4.0 but it doesn\'t seem like its working.

here is what I am doing

@Override
pu         


        
相关标签:
3条回答
  • 2020-12-07 04:56

    The super.onKeyDown(keyCode, event) is missing, i think you should try this:

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
    super.onKeyDown(keyCode, event);
    
       if (KeyCode == KeyEvent.KEYCODE.KEYCODE_APP_SWITCH && event.getRepeatCount() == 0)
        { 
            Log.d ("onkeydown","app switch key");
        }
       else if (KeyCode == KeyEvent.KEYCODE.KEYCODE_HOME && event.getRepeatCount() == 0)
        { 
            Log.d ("onkeydown","home key");
        }
        return true;
    }
    

    Doing the same using switch statement:

    @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
        super.onKeyDown(keyCode, event);
            switch(keyCode)
            {
                case KeyEvent.KEYCODE_CAMERA:
                 Toast.makeText(ListViewActivity.this, "Pressed Camera Button", Toast.LENGTH_SHORT).show();
                    return true;
                case KeyEvent.KEYCODE_1:
                 Toast.makeText(ListViewActivity.this, "Pressed 1", Toast.LENGTH_SHORT).show();
                    return true;
                case KeyEvent.KEYCODE_HOME:
                 Toast.makeText(ListViewActivity.this, "Pressed Home Button", Toast.LENGTH_SHORT).show();
                    return true;
    
                case KeyEvent.KEYCODE_BACK:
                 Toast.makeText(ListViewActivity.this, "Pressed Back Button", Toast.LENGTH_SHORT).show();
                    finish();
                    return true;
            }
    
            return false;
        }
    
    0 讨论(0)
  • 2020-12-07 04:58

    It can be achieved using reflection.

    It is under class android.os.ServiceManager having an aidl com.android.internal.statusbar.IStatusBarService and it contains toggleRecentApps method

    And also try this method here

    0 讨论(0)
  • 2020-12-07 05:00

    Um.

    Well, no, you can't.

    public static final int KEYCODE_HOME

    Since: API Level 1

    Key code constant: Home key.

    This key is handled by the framework and is never delivered to applications.

    http://developer.android.com/reference/android/view/KeyEvent.html

    0 讨论(0)
提交回复
热议问题