简体   繁体   English

如何在Android Wear应用程序的FragmentActivity中启用环境模式(始终打开)?

[英]How to enable Ambient Mode (Always On) in a FragmentActivity on an Android Wear App?

I have an android wear app that contains a FragmentActivity: 我有一个包含FragmentActivity的Android Wear应用:

public class MyFragmentActivity extends FragmentActivity

With 2 Fragments: 有2个片段:

public class MyFirstFragment extends Fragment

and

public class MySecondFragment extends Fragment

I want to enable the ambient mode (always on) in this fragment activity. 我想在此片段活动中启用环境模式(始终打开)。 However, according to the documentation , the ambient mode is only available if i extend WearableActivity . 但是,根据文档 ,仅当我扩展WearableActivity ,环境模式才可用。

Is there a way to have both properties of the FragmentActivity & WearableActivity together in one? 有没有办法将FragmentActivityWearableActivity两个属性合而为一?

or 要么

Is there another way to enable the ambient mode in the FragmentActivity ? 还有另一种方法可以在FragmentActivity启用环境模式吗?

You should implement AmbientModeSupport.AmbientCallbackProvider instead of WearableActivity , then you can extend FragmentActivity instead. 您应该实现AmbientModeSupport.AmbientCallbackProvider而不是WearableActivity ,然后可以扩展FragmentActivity

It is the new preferred method and it still gives you all the goodies you got with WearableActivity but also lets you use Activity (or any sub classes... FragementActivity, etc.). 它是新的首选方法,它仍然可以为您提供WearableActivity带来的所有好处,还可以让您使用Activity (或任何子类... FragementActivity等)。

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

Update: Use AmbientModeSupport now instead of AmbientMode . 更新:现在使用AmbientModeSupport而不是AmbientMode Google recently changed the name, so the old version is deprecated. Google最近更改了名称,因此不推荐使用旧版本。

Answer to both questions is no , sorry. 对两个问题的回答都是 ,对不起。

Ambient mode is enabled by calling WearableActivity.setUseAmbient() , which obviously is not available if you're not extending WearableActivity . 通过调用WearableActivity.setUseAmbient()启用环境模式,如果您不扩展WearableActivity ,则显然不可用。 And since Java doesn't support multiple inheritance, you can't subclass both WearableActivity and FragmentActivity at the same time - it's one or the other. 而且由于Java不支持多重继承,因此您不能同时将WearableActivityFragmentActivity子类化-这是一个或另一个。

Do you really need to be using Fragments on a watch activity? 您是否真的需要在观看活动中使用片段? If you really want to support ambient mode, you probably need to look at moving your UI out of fragments. 如果您确实想支持环境模式,则可能需要考虑将UI移出片段。

If you want to use Fragments on Android Wear and support Ambient mode, you need to use the AmbientModeSupport class. 如果要在Android Wear上使用片段并支持Ambient模式,则需要使用AmbientModeSupport类。

Make your activity extend FragmentActivity and implement AmbientModeSupport.AmbientCallbackProvider and you're all set! 让您的活动扩展FragmentActivity并实现AmbientModeSupport.AmbientCallbackProvider ,您就一切准备AmbientModeSupport.AmbientCallbackProvider

Details and examples are here: https://developer.android.com/training/wearables/apps/always-on#ambient-mode-class 详细信息和示例在这里: https : //developer.android.com/training/wearables/apps/always-on#ambient-mode-class

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

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