简体   繁体   English

从服务Android检测全屏

[英]Detect full screen from service Android

I need to detect the system ui visibility (full screen) from a service. 我需要从服务中检测系统ui可见性(全屏)。 I tried creating a custom view and adding setOnSystemUiVisibilityChangeListener but it never gets called. 我尝试创建一个自定义视图并添加setOnSystemUiVisibilityChangeListener但是它从未被调用。

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 . 我跑setListener从我服务的onStartCommand What's wrong here? 怎么了 Or is there any other method to detect when the system ui is visible or not? 还是有其他方法可以检测系统ui何时可见? Thanks for any help. 谢谢你的帮助。

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. Service中的自定义View未添加到绘制的视图层次结构中。 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. 在活动中,您可以执行mRootView.add(mCustomView) ,然后检查,但由于无法引用可见的布局,因此无法在Service

For a (hacky) solution see this answer: Receiving hidden status bar/entering a full screen activity event on a Service 对于(hacky)解决方案,请参见以下答案: 接收隐藏的状态栏/在服务上输入全屏活动事件

If you have an Activity visible while the Service is running there are more options. 如果在Service运行时可见Activity ,则有更多选项。 You can bind the Service to an Activity and send updates from a setOnSystemUiVisibility() inside the Activity to the Service . 您可以将Service绑定到Activity ,并将更新从Activity内的setOnSystemUiVisibility()发送到Service See: http://developer.android.com/guide/components/bound-services.html 请参阅: 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. 或只是在UI状态更改时从“ Activity发出广播, Service监听该广播。

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. 您可以添加一个视图(具有完整高度),并使用OnGlobalLayoutListener来更改列表器的布局,在onGlobalLayout中检查view.getHeight,屏幕的Y值上限为Y。

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

Hope help you 希望对你有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM