How to create a system overlay in Android which allows interaction with the windows below it?

a 夏天 提交于 2019-12-12 05:18:50

问题


As seen in this app, I want to create an app which dims the screen by creating a shaded overlay.

The window is created, and it's partially transparent, however, I cannot get the applications below it to launch. I can click them, and see the button presses, but other apps cannot launch while mine is running.

Suggestions?

I enclosed my code below, and an example of an app which is already doing this.

final WindowManager.LayoutParams params = new WindowManager.LayoutParams( 
    WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
    WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | 
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | 
    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
    WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
    PixelFormat.TRANSLUCENT);   

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

    LayoutInflater inflater = (LayoutInflater)   getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    ViewGroup mTopView = (ViewGroup) inflater.inflate(R.layout.activity_black, null);
    getWindow().setAttributes(params);
    wm.addView(mTopView, params);

https://play.google.com/store/apps/details?id=com.haxor


回答1:


This is an easy way:

Dialog dialog = new Dialog(this,android.R.style.Theme_DeviceDefault_Dialog_NoActionBar_MinWidth);
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);

dialog.getWindow().setFormat(PixelFormat.TRANSLUCENT);
dialog.getWindow().setDimAmount(0);
dialog.setContentView(new EditText(this));
dialog.show();



回答2:


Modified the Activity to be settings, and moved the blacking component to a service, which worked fine.



来源:https://stackoverflow.com/questions/14221892/how-to-create-a-system-overlay-in-android-which-allows-interaction-with-the-wind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!