How to override the Home key function

拜拜、爱过 提交于 2020-01-06 18:33:47

问题


I'm developing a pattern lock app.
The problem is that when the phone screen is off, then my LockActivity shows to be unlocked, but when i press the mobile home key, then it works.

I want to override the home key function (as it's not working) until I enter the unlock pattern.

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

        if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)||(keyCode == KeyEvent.KEYCODE_POWER)||(keyCode == KeyEvent.KEYCODE_VOLUME_UP)||(keyCode == KeyEvent.KEYCODE_CAMERA)) {

            return true; 
        }
       if((keyCode == KeyEvent.KEYCODE_HOME)){

           return true;
        }

    return false;

    }

    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_POWER ||(event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN)||(event.getKeyCode() == KeyEvent.KEYCODE_POWER)) {
            //Intent i = new Intent(this, NewActivity.class);
            //startActivity(i);
            return false;
        }
         if((event.getKeyCode() == KeyEvent.KEYCODE_HOME)){


           return true;
         }
    return false;
    }

I googled, but did not get any solution.
Please help me.


回答1:


There is no way to override the Home key as such. There is no way to check for the Home key pressed event as well. The only possibility you have is, to write your own Home or Launcher activity. To do so implement your activity, and register it as follows in the Manifest:

<activity
    android:name=".MyNewHomeActivity"
    android:launchMode="singleInstance">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.HOME" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

Once you have done this you can decide what to do in onCreate() or onResume(). Note that this requires your app/activity to be the default launcher.



来源:https://stackoverflow.com/questions/29920762/how-to-override-the-home-key-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!