Failed to call onClickListener in WindowManager

I implement windowManager in the Service class

WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); wm.addView(mView, params); 

I added a view to my window manager, but the listener for this view does not work.

  mView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(getBaseContext(),"onClick", Toast.LENGTH_LONG).show(); } }); 

Here are the layout options.

  WindowManager.LayoutParams params = new WindowManager.LayoutParams(130,130); params.type=WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY; params.flags=WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH; params.format=PixelFormat.TRANSPARENT; 

I want this listener to click in order to work normally, but it does not work, so please help me in this regard.

+4
source share
2 answers

Hi After a lot of research, I found this code. that should work. give it a try. Add this line to the on-create of your class of service. Below are the parameters that we must pass to FLAG_WATCH_OUTSIDE_TOUCH and that’s it. Hope this helps you.

 WindowManager.LayoutParams params = new WindowManager.LayoutParams(100, 100, 2007, 8, -3); Button bb=new Button(this); bb.setText("Button"); bb.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub System.out.println("Clicked----><<<<<<<"); } }); bb.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub System.out.println("Touched =----- > "); return false; } }); params.gravity = Gravity.RIGHT | Gravity.TOP; params.setTitle("Load Average"); WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); wm.addView(bb, params); 
+7
source

for me, I used this:

 WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY; 

and I found out that after android 4.0 this flag prevents clicks on the window manager due to security reasons

+1
source

All Articles