简体   繁体   English

Android Wear检测环境屏幕模式

[英]Android Wear detect ambient screen mode

Is there a way to detect when the watch is in ambient screen mode? 有没有办法检测手表何时处于环境屏幕模式? I make a watch face and want to continue to update the clock when the ambient screen mode is on (clock is shown on screen), but I want to stop updating when the screen is off. 我做了一个表盘,想在环境屏幕模式打开时继续更新时钟(屏幕上显示时钟),但是我想在屏幕关闭时停止更新时钟。 For now, I start and stop the update in onPause and onResume methods, but the onPause method is called when the ambient screen mode comes on. 现在,我在onPauseonResume方法中启动和停止更新,但是在环境屏幕模式打开时会调用onPause方法。

Thanks. 谢谢。

To be notified of the Ambient mode changed, you have to use a DisplayListener . 要通知Ambient模式已更改,您必须使用DisplayListener You can find the way to do that here 您可以在这里找到解决方法

The normal way is to use android WearableActivity and implement onEnterAmbient and onExitAmbient . 正常方法是使用android WearableActivity并实现onEnterAmbientonExitAmbient

And of cause, there's another way, use DisplayListener to check whether display state is DOZE or DOZE_SUSPEND . 当然,还有另一种方法,使用DisplayListener来检查显示状态是DOZE还是DOZE_SUSPEND

You should implement the AmbientMode.AmbientCallbackProvider callback instead. 您应该改为实现AmbientMode.AmbientCallbackProvider回调。

It is the new preferred method and it still gives you the onEnterAmbient() , onAmbientUpdate() , and onExitAmbient() but also lets you use Activity (or any sub classes... FragementActivity, etc.). 这是新的首选方法,它仍然为您提供onEnterAmbient()onAmbientUpdate()onExitAmbient()但也允许您使用Activity (或任何子类... FragementActivity等)。 It also allows you to support the Architecture components. 它还允许您支持体系结构组件。

Official docs call out the details (and example code) : 官方文档标注了详细信息(和示例代码)

public class MainActivity extends Activity implements AmbientMode.AmbientCallbackProvider {

    /*
     * Declare an ambient mode controller, which will be used by
     * the activity to determine if the current mode is ambient.
     */
    private AmbientMode.AmbientController mAmbientController;
    …
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    ...
        mAmbientController = AmbientMode.attachAmbientSupport(this);
    }
    ...

    …
    @Override
    public AmbientMode.AmbientCallback getAmbientCallback() {
        return new MyAmbientCallback();
    }
    …

private class MyAmbientCallback extends AmbientMode.AmbientCallback {
    @Override
    public void onEnterAmbient(Bundle ambientDetails) {
             // Handle entering ambient mode
    }

    @Override
    public void onExitAmbient() {
      // Handle exiting ambient mode
     }

    @Override
    public void onUpdateAmbient() {
      // Update the content
    }
}

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

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