我目前开发了一款安装了android的表盘。然而,我现在想在主机应用程序上创建一个允许用户自定义表盘的设置部分。我是android开发的新手,所以我很好奇这样做的正确方法。
有没有办法在主机上更新共享首选项,然后在磨损设备上推送或同步共享首选项?或者我应该看到一个完全不同的方式吗?
答案 0 :(得分:14)
您可以使用DataApi
或MessageApi
在手机和手表设备之间同步您的表盘配置。
请查看文档并选择更符合您需求的文档:
https://developer.android.com/training/wearables/data-layer/index.html
https://developer.android.com/training/wearables/data-layer/data-items.html
https://developer.android.com/training/wearables/data-layer/messages.html
以下是使用DataApi
。
推送到DataApi
的所有内容都在设备之间共享,并且可以同时使用。您可以双方更改此数据,另一方将立即通知此类更改(当设备相互连接时)。您也可以随时读取此数据(例如,当用户在Watch上选择您的表盘时 - 配置数据将在那里等待您。)
public class WatchfaceConfigActivity extends Activity {
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(new ConnectionCallbacks() {
@Override
public void onConnected(Bundle connectionHint) {
}
@Override
public void onConnectionSuspended(int cause) {
}
})
.addOnConnectionFailedListener(new OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
}
})
.addApi(Wearable.API)
.build();
mGoogleApiClient.connect();
}
每当您想要将新配置与Android Wear设备同步时,您必须通过可穿戴设备DataApi
放置DataRequest:
private void syncConfiguration() {
if(mGoogleApiClient==null)
return;
final PutDataMapRequest putRequest = PutDataMapRequest.create("/CONFIG");
final DataMap map = putRequest.getDataMap();
map.putInt("mode", 1);
map.putInt("color", Color.RED);
map.putString("string_example", "MyWatchface");
Wearable.DataApi.putDataItem(mGoogleApiClient, putRequest.asPutDataRequest());
}
}
您需要创建一个扩展WearableListenerService
的类:
public class DataLayerListenerService extends WearableListenerService {
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
super.onDataChanged(dataEvents);
final List<DataEvent> events = FreezableUtils.freezeIterable(dataEvents);
for(DataEvent event : events) {
final Uri uri = event.getDataItem().getUri();
final String path = uri!=null ? uri.getPath() : null;
if("/CONFIG".equals(path)) {
final DataMap map = DataMapItem.fromDataItem(event.getDataItem()).getDataMap();
// read your values from map:
int mode = map.getInt("mode");
int color = map.getInt("color");
String stringExample = map.getString("string_example");
}
}
}
}
并在AndroidManifest
:
<service android:name=".DataLayerListenerService" >
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
请注意,这只是一个使用示例。也许(而不是注册WearableListenerService
的实例),您最好直接在Watchface中创建mGoogleApiClient
的实例并在其中添加DataListener
:
Wearable.DataApi.addListener(mGoogleApiClient, new DataListener() {
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
// read config here and update the watchface
}
});
也许您不需要共享数据 - 然后您可以使用MessageApi
进行通信,并仅在保存新配置时发送消息,或者观看想要从手机读取当前配置。
答案 1 :(得分:1)
移动设备和佩戴模块本身没有共享首选项,但您可以发送消息和/或更新侦听器将检测到的资产。例如,每当您更改手机上的首选项时,您还可以使用Message API向手表发送消息。在观看时,您应该使用WearableListenerService
方法实施onMessageReceived
,您可以在其中解析消息并采取适当的操作,例如在手表上设置偏好。
查看Android开发者培训指南:https://developer.android.com/training/wearables/data-layer/index.html