如何像锁定屏幕应用程序一样在Android中禁用主页按钮? [英] How to disable home button in Android like lock screen apps do?

查看:124
本文介绍了如何像锁定屏幕应用程序一样在Android中禁用主页按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经问过很多次了,但是我发现所有解决方案都没有用. 我尝试了下面给出的代码...

I know this question is asked many times but I found that none of the solution is working. I tried the code given below...

   protected void onPause() {
   super.onPause();
    Intent intent = new Intent(this,LockActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    }

它的作用是,当启动Android主屏幕时,它将当前活动重新显示在最前面,但是当启动主屏幕时,将活动重新显示在前面需要将近3-4秒.

What it does is that it bring the current activity again to front when android home screen is launched but it takes almost 3-4 seconds to bring activity againt to front when home screen is launched.

我使用了一些锁定屏幕应用程序,这些应用程序甚至在单击主页"按钮时都无法启动主屏幕.我想实现这样的目标.

I have used some lock screen apps which don't even start the home screen when home button is clicked. I want to achieve something like that.

我也使用过onUserLeavesHint方法,onKeyDown方法和onKeyDispatch方法,但是它们都不适合我.

I have also used onUserLeavesHint method, onKeyDown method and onKeyDispatch method but none of them worked for me.

并且请勿回答或发表评论,因为无法在Android中禁用主页"按钮.对于此类答案或评论,我建议您通过PlayStore上的一些锁屏应用程序.我也在github上找到了一个可以正常工作的应用程序以及源代码.它可以在我的手机上运行,​​并且该应用程序使用了disableKeyguard,但是当我在应用程序中执行相同操作时,它不起作用(disableKeyguard已弃用,但我使用@supress warnings("deprecation")).

And please don't answer or comment like it is not possible to disable home button in Android. For such answers or comments I would suggest you to go through some Lock Screen apps on PlayStore. Also I found a working app on github along source code. It was working on my phone and the app used disableKeyguard but when I do the same in my app it doesn't work (disableKeyguard is deprecated but I use @supress warnings("deprecation")).

推荐答案

源- https://github.com/shaobin0604/Android-HomeKey-Locker

//Copy this class
public class HomeKeyLocker {
    private OverlayDialog mOverlayDialog;
    public void lock(Activity activity) {
        if (mOverlayDialog == null) {
            mOverlayDialog = new OverlayDialog(activity);
            mOverlayDialog.show();
        }
    }
    public void unlock() {
        if (mOverlayDialog != null) {
            mOverlayDialog.dismiss();
            mOverlayDialog = null;
        }
    }
    private static class OverlayDialog extends AlertDialog {

        public OverlayDialog(Activity activity) {
            super(activity, R.style.OverlayDialog);
            WindowManager.LayoutParams params = getWindow().getAttributes();
            params.type =  WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
            params.dimAmount = 0.0F; // transparent
            params.width = 0;
            params.height = 0;
            params.gravity = Gravity.BOTTOM;
            getWindow().setAttributes(params);
            getWindow().setFlags( WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |  WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, 0xffffff);
            setOwnerActivity(activity);
            setCancelable(false);
        }

        public final boolean dispatchTouchEvent(MotionEvent motionevent) {
            return true;
        }

        protected final void onCreate(Bundle bundle) {
            super.onCreate(bundle);
            FrameLayout framelayout = new FrameLayout(getContext());
            framelayout.setBackgroundColor(0);
            setContentView(framelayout);
        }
    }
}

//Paste this in your activity
mHomeKeyLocker = new HomeKeyLocker();
mHomeKeyLocker.lock(this);

这篇关于如何像锁定屏幕应用程序一样在Android中禁用主页按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆