Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable home button in Android like lock screen apps do?

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);
    }

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.

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

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")).

like image 543
Ruag Avatar asked Apr 03 '16 08:04

Ruag


People also ask

How do I disable Home button on Android?

Navigate to Android > Restrictions > Basic and click on Configure. Under Allow Device Functionality, you'll have the options to disable Home/Power button. Home Button-Uncheck this option to restrict users from using the Home Button. Power Off-Uncheck this option to restrict users from turning their devices off.


1 Answers

source - 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);
like image 52
NitinM Avatar answered Sep 17 '22 15:09

NitinM