Creating a system overlay window (always on top)

前端 未结 16 971
南方客
南方客 2020-11-21 07:07

I\'m trying to create an always-op-top button/clickable-image which stays on top of all the windows all the time.

The proof of concept is

  • here - Smar
16条回答
  •  遇见更好的自我
    2020-11-21 07:30

    Here is some simple solution, All you need is to inflate XML layout just like you do on list adapters, just make XML layout to inflate it. Here is code that all you need.

     public class HUD extends Service {
        View mView;
    
        LayoutInflater inflate;
        TextView t;
        Button b;
    
        @Override
        public void onCreate() {
            super.onCreate();   
    
            Toast.makeText(getBaseContext(),"onCreate", Toast.LENGTH_LONG).show();
    
    
            WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    
            Display display = wm.getDefaultDisplay();  get phone display size
            int width = display.getWidth();  // deprecated - get phone display width
            int height = display.getHeight(); // deprecated - get phone display height 
    
    
            WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    width, 
                    height,
                    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    |WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                     PixelFormat.TRANSLUCENT);
    
    
            params.gravity = Gravity.LEFT | Gravity.CENTER;
            params.setTitle("Load Average");
    
            inflate = (LayoutInflater) getBaseContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            mView = inflate.inflate(R.layout.canvas, null);
    
            b =  (Button) mView.findViewById(R.id.button1);
            t = (TextView) mView.findViewById(R.id.textView1);
            b.setOnClickListener(new OnClickListener() {
    
            public void onClick(View v) {
                // TODO Auto-generated method stub
                t.setText("yes you click me ");
            }
           });
    
            wm.addView(mView, params);
    
            }
    
    
    
        @Override
        public IBinder onBind(Intent arg0) {
            // TODO Auto-generated method stub
            return null;
        }
    
    }
    

提交回复
热议问题