Man Manam Man Manam - 1 year ago 127
Android Question

Lock application with knock code using always on top Activity

I am new in Android. I am developing a kind of app locker. I want to start a fullscreen always on top

that listens to
, when the user starts a locked app. I want to make user to get rid of that
only if he/she enters a knock code as password. I have used following code to lock buttons by making
always on top (Source: Creating a system overlay window (always on top)). But
does not receives any touches for knock code.
I have searched a lot and tried a lot of codes. But always one solution destroys other parts like current problem!

final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT ,

WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

ViewGroup mTopView = (ViewGroup) getLayoutInflater().inflate(R.layout.activity_color, null);
wm.addView(mTopView, params);

Answer Source

onTouchEvent() does not work because we have an overlay on top of everything. So when we touch screen, we touch ViewGroup. So we need to trigger onTouchEvent() when user touches ViewGroup. We can trigger it like this:

mTopView.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View arg0, MotionEvent event) 
                   // do something ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download