Android full screen detection

I need to detect a visibility system (full screen) from a service. I tried to create a custom view and add setOnSystemUiVisibilityChangeListener , but it will never be called.

 public void setListener() { setOnSystemUiVisibilityChangeListener(new OnSystemUiVisibilityChangeListener() { @Override public void onSystemUiVisibilityChange(int i) { Log.e(TAG, "onSystemUiVisibilityChange =" + i); } }); } 

I am running setListener from my onStartCommand service. What is wrong here? Or is there any other method to detect when the ui system is visible or not? Thanks for any help.

+1
source share
2 answers

You wrote:

... from service. I tried to create a custom view ...

The custom View in Service not added to the View hierarchy that was drawn. Therefore, the method is not called. In Activity, you can do mRootView.add(mCustomView) and then check, but this does not work with Service , since you have no link to the visible layout.

To solve the (hacker) solution, see this answer: Getting a hidden status bar / entering a full-screen activity event in the service

If you have an Activity while the Service is running, there are more options. You can bind Service to Activity and send updates with setOnSystemUiVisibility() inside Activity to Service . See: http://developer.android.com/guide/components/bound-services.html

Or just send a broadcast from Activity when the state of the user interface that the Service is listening on changes.

+1
source

You can add one view (with full height) and use OnGlobalLayoutListener to change the Listerner layout, in onGlobalLayout check view.getHeight from max Y screen.

 if (mMaxY == helperWnd.getHeight()) { //full screen } else { //none full screen } 

Hope to help you.

+1
source

Source: https://habr.com/ru/post/1499759/


All Articles