为Watch Face添加简单的切换配置(Android Wear)

时间:2014-12-18 03:20:57

标签: android wear-os

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

我希望切换开关能够在手表上使用,不希望与手机通信以进行如此简单的操作,并希望避免手表和手机之间的所有GoogleApiClient通信。有没有办法轻松完成此操作,类似于在Android上执行设置或SharedPreferences?

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

2 个答案:

答案 0 :(得分:3)

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

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

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

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

IntentFilterActivity

<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”。

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

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

答案 1 :(得分:1)

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

  • 获取由Watch Service预期的Config Activity发送的初始数据
  • 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;
    }
}