Disable Home button in Android 4.0+

久未见 提交于 2019-12-17 09:57:55

问题


I'm trying to replace the stock lock screen with my own app.
In my code, i want to disable the Home button.
I know how to do this in Android 2.3 and below,
but the same code doesn't work with Android 4.0+ (return to desktop when Home button pressed)

Recently I found out an app called MiHome which has its own lock screen and is able to disable the Home button.
Does anyone know how it achieves this???


回答1:


There is no way to intercept the home button on Android, unless you make your app the home screen. This is for security reasons, so that malicious apps cannot take over your device by overriding all the buttons that can exit. The home button is the one sure shot way to be able to leave any app.

If you want to handle the HOME button, implement a home screen.




回答2:


Mucking with the home button is disabled for security/reliability reasons in ICS. (Most apps use it for evil than good)

Please refer to the following questions for workarounds.

  1. how can I disable android 4.0 home button
  2. override Home key in android ICS
  3. Disable Home Button in Android ICS (4.0)

Seems like the only way is to implement a home screen




回答3:


Try this

   params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
                    PixelFormat.TRANSLUCENT);
            params.gravity = Gravity.TOP;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            } else {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }
    mOverlay = (RelativeLayout) inflater
                    .inflate(R.layout.main, (ViewGroup) null);

    mOverlay.setFitsSystemWindows(false);
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
            mWindowManager = (WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE);
            mWindowManager.addView(mOverlay, params);


来源:https://stackoverflow.com/questions/15459407/disable-home-button-in-android-4-0

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