Reputation: 165
I started developing my first Android Wear watchface and have some problems understanding the concept of settings.
The user of my watchface should be able to set some preferences right on the watch - e.g. different colors.
But how should these settings be persisted on the watch? All the samples from Google use the Wearable.NodeApi.getLocalNode. But as far as I could understand, this is intended to synchronize settings between different(?) watches via the Internet. At least according to the guide on https://developer.android.com/training/wearables/data-layer/index.html
But does this mechanism also make the settings persistant, or do I have to implement the persistency myself (e.g. using SharedPreferences).
My view on this topic is, that if I change these settings e.g. via an UI on my phone, they get synchronized to all my watches via the DataItems - but on every watch I then have to store these settings. Is this correct?
Upvotes: 1
Views: 550
Reputation: 6635
In general, the DataApi
will persist data items locally between sessions - but it's less reliable, and a lot less convenient, than using SharedPreferences
. As you noted, it's mostly meant for synchronizing data between devices; storing data is an afterthought. In my experience, data storage and synchronization are different enough that you need to implement both separately, using the APIs meant for each.
However, there's no sense reinventing the wheel. If your watch face will have configuration on both devices that needs both syncing to the other device and storing locally, may I suggest that you look at PrefSyncService. It's an open-source Android class that I designed for just this sort of use case.
To apply config changes from the phone to your running watch face, use an OnSharedPreferenceChangeListener
. In your Engine
, use code something like this:
private class Engine
extends CanvasWatchFaceService.Engine
implements SharedPreferences.OnSharedPreferenceChangeListener {
private SharedPreferences settings;
@Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
settings.registerOnSharedPreferenceChangeListener(this);
...other setup code...
}
@Override
public void onDestroy() {
settings.unregisterOnSharedPreferenceChangeListener(this);
...other teardown code...
super.onDestroy();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences settings, String key) {
...update your watch face here...
}
}
Upvotes: 1