Android: Overlay on Window over Activities of a Task

前端 未结 2 739
南旧
南旧 2021-02-06 06:04

I wanted to create an Overlay, like a HUD, that resides on the screen during my applications activity stack (the task of my app) changes.

I found a couple of examples u

2条回答
  •  不思量自难忘°
    2021-02-06 06:45

    You really only have two options.

    1) You can create an activity with the theme of Theme.Dialog. This will display a popup on top of your window. You can create the dialog to be modal-less (can click through). In my quick testing I wasn't able to get overlay to the edges of my screen although maybe modifying the theme would fix that.

    MainActivity.java

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button test = (Button) this.findViewById(R.id.test);
            test.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((Button) v).setBackgroundColor(Color.RED);
                }
            });
    
            Intent i = new Intent(this, SecondActivity.class);
            startActivity(i);
        }
    }
    

    SecondActivity.java

    public class SecondActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second_main);
    
            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
            window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
            window.setGravity(Gravity.BOTTOM);
        }
    
        @Override
        public void onBackPressed() {
                //Override to prevent back button from closing the second activity dialog
        }
    }
    

    Manifest

        ....
        
            
                
                
            
        
        
            ....
        
        ....
    

    2) The second option is to use a SYSTEM_ALERT_WINDOW. I prefer this method significantly more. You are correct that it CAN be visible and on top of every other app, however, you can control when it is visible and when it isn't. I'm not going to post any source code but I will give you a general plan of attack.

    When you create the service, bind to it using an AIDL. This way you'll be able to talk directly with the service to tell it when to 'hide' and 'show' the overlay. Speaking of hiding and showing, onPause and onResume can be used to tell the service to hide and show the overlay. Lastly, if you need to receive click events on your overlay, that will prove to be tricky as the touch events don't always act the way you expect them to.

    Good Luck.

提交回复
热议问题