Man Manam Man Manam - 2 months ago 20
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

Activity
that listens to
onTouchEvent()
, when the user starts a locked app. I want to make user to get rid of that
Activity
only if he/she enters a knock code as password. I have used following code to lock buttons by making
Activity
always on top (Source: Creating a system overlay window (always on top)). But
onTouchEvent()
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.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT ,
WindowManager.LayoutParams.FLAG_FULLSCREEN,
PixelFormat.TRANSPARENT);

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

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

Answer

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() {
            @Override
            public boolean onTouch(View arg0, MotionEvent event) 
            {  
                   // do something ...
            }
});