Android Wear detect ambient screen mode

六月ゝ 毕业季﹏ 提交于 2019-12-13 18:28:35

问题


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.

Thanks.


回答1:


To be notified of the Ambient mode changed, you have to use a DisplayListener. You can find the way to do that here




回答2:


The normal way is to use android WearableActivity and implement onEnterAmbient and onExitAmbient.

And of cause, there's another way, use DisplayListener to check whether display state is DOZE or DOZE_SUSPEND.




回答3:


You should implement the AmbientMode.AmbientCallbackProvider callback instead.

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.). 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
    }
}


来源:https://stackoverflow.com/questions/27184148/android-wear-detect-ambient-screen-mode

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