0

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.

Hammad Khan
  • 157
  • 2
  • 13

2 Answers2

1

You need to override on your Activity OnKeyDown, as Shark said. Simply copy & paste below code to your activity.

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        if( (keyCode==KeyEvent.KEYCODE_HOME)
        {
            // *** DO YOUR STUFF HERE ***
            return true;
        }
           else
             return super.onKeyDown(keyCode, event);
    }

It will handle the click on home button.

Nawako
  • 342
  • 3
  • 17
1

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);
}
Hammad Khan
  • 157
  • 2
  • 13