首页 > 代码库 > WindowManger

WindowManger


  1. create a windowmanger,which can receive key events,and do not prevent the events.

public class MainActivity extends AppCompatActivity

{

   private WindowManager mWindowManager;
   private WindowManager.LayoutParams mLParams;
   private ImageView mPopupView;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       initWM();
       initPopupView();
   }

   @Override
   protected void onDestroy() {
       super.onDestroy();
       mWindowManager.removeView(mPopupView);
   }

   private void initWM()
   {
       mWindowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
       mLParams = new WindowManager.LayoutParams(
               WindowManager.LayoutParams.WRAP_CONTENT, //w
               WindowManager.LayoutParams.WRAP_CONTENT, //h
               WindowManager.LayoutParams.TYPE_PHONE, //type,设置显示的类型,TYPE_PHONE指的是来电话的时候会被覆盖,其他时候会在最前端显示

               WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON  //flag
                       | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                       | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, //不阻塞事件传递到后面的窗口
               PixelFormat.TRANSLUCENT // 不设置这个弹出框的透明遮罩显示为黑色
       );
       //设置对齐方式
       mLParams.gravity = Gravity.CENTER;
       //设置显示的位置
       mLParams.x = 0;  //base on gravity
       mLParams.y = 0;
   }
   
   private void initPopupView()
   {
       mPopupView = new ImageView(this);
       mPopupView.setImageResource(R.mipmap.ic_launcher);
//        mPopupView.setFocusable(true);
       mPopupView.setFocusableInTouchMode(true);  //enable receive key events
       mPopupView.setOnKeyListener(new View.OnKeyListener() {
           @Override
           public boolean onKey(View view, int keyCode, KeyEvent event) {
               if (keyCode == KeyEvent.KEYCODE_BACK
                       && event.getRepeatCount() == 0
                       && event.getAction() == KeyEvent.ACTION_UP) {
                   Toast.makeText(MainActivity.this, "press back key", Toast.LENGTH_SHORT).show();
               } else if (keyCode == KeyEvent.KEYCODE_ENTER
                       && event.getRepeatCount() == 0
                       && event.getAction() == KeyEvent.ACTION_UP) {
                   Toast.makeText(MainActivity.this, "press enter key", Toast.LENGTH_SHORT).show();

               } else {
                   Toast.makeText(MainActivity.this, "press key:keyCode="+keyCode, Toast.LENGTH_SHORT).show();

               }
               return false;
           }
       });
   }

   private boolean mbVisible;

   public void onClick(View view) {
       if (mbVisible = !mbVisible) {
           mWindowManager.addView(mPopupView, mLParams);  //show View
       } else {
           mWindowManager.removeViewImmediate(mPopupView);  //remove View
       }
   }
   

}


本文出自 “whatever957” 博客,请务必保留此出处http://whatever957.blog.51cto.com/6835003/1856820

WindowManger