Alex Newman
Alex Newman

Reputation: 1397

Detect full screen from service Android

I need to detect the system ui visibility (full screen) from a service. I tried creating a custom view and adding setOnSystemUiVisibilityChangeListener but it never gets called.

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

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

Upvotes: 1

Views: 1446

Answers (2)

Hoa Le
Hoa Le

Reputation: 2454

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

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

Hope help you

Upvotes: 1

Gunnar Karlsson
Gunnar Karlsson

Reputation: 28470

You wrote:

...from a service. I tried creating a custom view...

The custom View in your Service in not added to the View hierarchy that's drawn. That's why the method is not called. In an Activity you could do mRootView.add(mCustomView) and then check but that doesn't work from a Service since you don't have a reference to a visible layout.

For a (hacky) solution see this answer: Receiving hidden status bar/entering a full screen activity event on a Service

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

Or just send out a broadcast from the Activity when the UI state changes, which the Service listens for.

Upvotes: 1

Related Questions