【问题标题】:Adding a simple toggle configuration for Watch Face (Android Wear)为 Watch Face (Android Wear) 添加一个简单的切换配置
【发布时间】:2015-03-16 15:54:27
【问题描述】:

在为 Android Wear 创建表盘时,我想要一个简单的配置(拨动开关?)来设置用户希望表盘看起来像哪种模式(例如,白色或黑色表盘)。

我希望拨动开关位于手表本身,不希望与手机进行如此简单的通信,并希望避免手表和手机之间的所有 GoogleApiClient 通信。有没有办法轻松做到这一点,类似于在 Android 上进行 Settings 或 SharedPreferences?

我尝试使用广播接收器。我可以在广播接收器中获取更改,但如何让 CanvasWatchFaceService.Engine 更新?

【问题讨论】:

    标签: android wear-os


    【解决方案1】:

    是的,这是可能的。 你必须关注this documentation

    首先创建一个Activity,显示您希望用户更改的设置。

    然后在您的 Manifest 文件中,将此元数据添加到您的 Watchface 服务:

    <meta-data
        android:name=
           "com.google.android.wearable.watchface.wearableConfigurationAction"
        android:value=
           "com.example.android.wearable.watchface.CONFIG_DIGITAL" />
    

    还有这个IntentFilter 给你的Activity

    <intent-filter>
            <action android:name=
                "com.example.android.wearable.watchface.CONFIG_DIGITAL" />
            <category android:name=
           "com.google.android.wearable.watchface.category.WEARABLE_CONFIGURATION" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    

    当然,您必须将“com.example.android”替换为您的包名。

    然后,在表盘选择器屏幕的表盘预览下方会出现一个小的设置图标。

    不要忘记同步 Activity 和 Watchface 之间的设置,以使其立即显示(例如 BroadcastReceiver

    【讨论】:

    • 感谢您的帮助。我知道可以进行可穿戴配置部分,我要问的是最后一部分“活动和表盘之间的同步”(有关此的任何信息/文档)?
    • 没有通用的解决方案。您可以在表盘中使用广播接收器,并在您的活动中发送广播意图,其中包含您的额外值。
    • 谢谢,我试试看。
    • 我试过了。将广播接收器放在表盘服务或引擎中,两者似乎都没有收到发送的消息。我把我的示例代码放在问题中。
    • 按照惯例,你的动作应该从你的包名开始。您是否在清单中的服务的意图过滤器中添加了此操作?
    【解决方案2】:

    我通过注册 3 个意图的 LocalBroadcastManager 解决了这个问题

    • 获取初始数据,从 Config Activity 发送,由 Watch Service 预期
    • Watch Service 为响应上述消息而发送的初始数据
    • 数据已更改,由 Config Activity 在用户进行选择时发送。

    所有东西都封装在一个类中,该类公开了两个交互接口(一个用于 Watch Service,另一个用于 Config Activity。可能不是最简单的解决方案,但经过 3 天的挖掘,我能想出最好的解决方案 :(

    作为记录,这里是共享 2 个变量(bezelMode 和 time24)的类。
    您将需要从您的监视服务(实现 WatchConfig.Service)和配置活动(实现 WatchConfig.Editor)中实例化它
    通信基于LocalBroadcastManager

    public class WatchConfig {
    //    private static final String TAG = "Config";
    
        // Used when data has changed
        public static final String CONFIG_DATA_CHANGED = "/config/changed";
        // Used to provide initial data
        public static final String CONFIG_INITIAL_DATA = "/config/inital-data";
        // Used to query initial data
        public static final String CONFIG_INITIAL_QUERY = "/config/initial-query";
    
        private int m_BezelMode;
        private boolean m_Time24;
    
        private LocalBroadcastManager localBroadcastManager;
        BroadcastReceiver broadcastReceiverDataChanged;
        BroadcastReceiver broadcastReceiverInitialDataRequest;
        BroadcastReceiver broadcastReceiverInitialData;
    
        private Service service;
        private Editor editor;
    
        WatchConfig(Context context, Service service) {
            initialize( context, service, null);
        }
    
        WatchConfig(Context context, Editor editor) {
            initialize( context, null, editor);
        }
    
        void initialize( Context context, Service service, Editor editor) {
            this.localBroadcastManager = LocalBroadcastManager.getInstance( context);
    
            this.service = service;
            this.editor = editor;
        }
    
        interface Service {
            void onConfigDataUpdated(boolean time24, int bezelMode);
            void onConfigInitialRequest();
        }
    
        interface Editor {
            void onConfigInitialize(boolean time24, int bezelMode);
        }
    
        /**
         * Registers all proper receivers
         */
        public void connect() {
            if( this.service != null) {
                IntentFilter intentFilterDataChanged = new IntentFilter(CONFIG_DATA_CHANGED);
                this.broadcastReceiverDataChanged = new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
    //                    Log.d(TAG,"Data Changed Notification");
                        service.onConfigDataUpdated(intent.getBooleanExtra("time24", true), intent.getIntExtra("bezel", 24));
                    }
                };
                this.localBroadcastManager.registerReceiver(broadcastReceiverDataChanged, intentFilterDataChanged);
    
                IntentFilter intentFilterInitialDataRequesy = new IntentFilter(CONFIG_INITIAL_QUERY);
                this.broadcastReceiverInitialDataRequest = new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
    //                    Log.d(TAG,"Initial Query Notification");
                        service.onConfigInitialRequest();
                    }
                };
                this.localBroadcastManager.registerReceiver(broadcastReceiverInitialDataRequest, intentFilterInitialDataRequesy);
            } else {
                this.broadcastReceiverDataChanged = null;
                this.broadcastReceiverInitialDataRequest = null;
            }
            if( this.editor != null) {
                IntentFilter intentFilterInitalData = new IntentFilter(CONFIG_INITIAL_DATA);
                this.broadcastReceiverInitialData = new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
    //                    Log.d(TAG,"Initial Data notification");
                        editor.onConfigInitialize(intent.getBooleanExtra("time24", true), intent.getIntExtra("bezel", 24));
                    }
                };
                this.localBroadcastManager.registerReceiver(broadcastReceiverInitialData, intentFilterInitalData);
    
                // Editors need intial data
                Intent intentInitialDataRequest = new Intent( CONFIG_INITIAL_QUERY);
                this.localBroadcastManager.sendBroadcast( intentInitialDataRequest);
            } else {
                this.broadcastReceiverInitialData = null;
            }
        }
    
        public void disconnect() {
            if( this.broadcastReceiverDataChanged != null) {
                this.localBroadcastManager.unregisterReceiver(this.broadcastReceiverDataChanged);
            }
            if( this.broadcastReceiverInitialDataRequest != null) {
                this.localBroadcastManager.unregisterReceiver(this.broadcastReceiverInitialDataRequest);
            }
            if( this.broadcastReceiverInitialData != null) {
                this.localBroadcastManager.unregisterReceiver(this.broadcastReceiverInitialData);
            }
        }
    
        /**
         * Used to publish changes in configuration
         */
        protected void publishInitialData(boolean time24, int bezel) {
            this.m_Time24 = time24;
            this.m_BezelMode = bezel;
    
            Intent intent = new Intent( CONFIG_INITIAL_DATA);
            intent.putExtra("time24", this.m_Time24);
            intent.putExtra("bezel", this.m_BezelMode);
            this.localBroadcastManager.sendBroadcast(intent);
        }
    
        /**
         * Used to publish changes in configuration
         */
        protected void publishUpdate() {
            Intent intent = new Intent( CONFIG_DATA_CHANGED);
            intent.putExtra("time24", this.m_Time24);
            intent.putExtra("bezel", this.m_BezelMode);
            this.localBroadcastManager.sendBroadcast(intent);
        }
    
        public void setTime24(boolean time24) {
            this.m_Time24 = time24;
        }
    
        public void setBezelMode(int bezelMode) {
            this.m_BezelMode = bezelMode;
        }
    }
    

    【讨论】:

    • 嘿太棒了!感谢分享。我一直在寻找类似的东西。很好的答案,虽然它更科普,但将两者结合起来很棒!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多