如何像锁定屏幕应用程序一样在Android中禁用主页按钮?

编程入门 行业动态 更新时间:2024-10-24 02:02:22
本文介绍了如何像锁定屏幕应用程序一样在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")).

推荐答案

源- github/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中禁用主页按钮?

本文发布于:2023-11-27 03:07:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636315.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   按钮   屏幕   主页   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!