Search code examples
androidlockscreen

Modify home button behaviour for lock screens in android


I am developing a Lock Screen App in android studio. I know we can not disable HOME button. But there are so many lock screen applications on play store. How are they doing it? When I press home button in those apps nothing happens. Type keyguard is deprecated and also keylocker. Please, tell me how can I make home button to do nothing or to be on the same screen. Now, I don't want to make it as Home Screen because it gives user a choice to select between home screens and I am developing a Lock Screen App so if this is the only solution then tell me how to make my screen as home screen programmatically. Please, give the right answer.


Solution

  • After a lot of search on google. I finally found a solution. I found it on GitHub. If some one is interested. Just copy the following class:

    your package name;
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.MotionEvent;
    import android.view.WindowManager;
    import android.widget.FrameLayout;
    import com.amigo.hammad.screenlock7292016.R;
    
    import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
    import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
    import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
    import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
    
    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.AppTheme);
            WindowManager.LayoutParams params = getWindow().getAttributes();
            params.type = TYPE_SYSTEM_ERROR;
            params.dimAmount = 0.0F; // transparent
            params.width = 0;
            params.height = 0;
            params.gravity = Gravity.BOTTOM;
            getWindow().setAttributes(params);
            getWindow().setFlags(FLAG_SHOW_WHEN_LOCKED | 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);
        }
    }
    }
    

    You may find an error on super(activity, R.style.AppTheme); this line. You just need to give your app theme.

    And then you need to make an Object in the onCreate Method of lock screen. And call the lock function. e.g:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Object of Class HomeKeyLocker.
        mHomeKeyLocker = new HomeKeyLocker();
        setContentView(R.layout.yourActivityLayoutName);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        // Calling Function in Class HomeKeyLocker to Block Home Button on this Activity.
        mHomeKeyLocker.lock(this);
    }