我下载了一个现成的手机应用程序并试图为我的Android服装项目实现一个表盘。目标是将温度信息从手机传递到磨损设备并让它更新表面。
我试过了: 1.相同的包装名称 2.相同的applicationId 3.相同的依赖版本 4.相同的权限 5.在我的手机端,我确信每次我的数据都不同(所以更新必须发生)
我也使用数据项,因此我的数据必须同步。
private void syncWatch(String min, String max, int image){
Log.v("SunshineSyncAdapter", "syncWatch");
String time = String.valueOf(new Date().getTime());
PutDataMapRequest putDataMapRequest = PutDataMapRequest.create("/weather-update");
putDataMapRequest.getDataMap().putLong("time", new Date().getTime()); // MOST IMPORTANT LINE FOR TIMESTAMP
putDataMapRequest.getDataMap().putString("min-temp", min + time);
putDataMapRequest.getDataMap().putString("max-temp", max + time);
Log.v("SunshineSyncAdapter", min + time + " " + max + time);
PutDataRequest request = putDataMapRequest.asPutDataRequest();
if (mGoogleApiClient == null){
Log.v("SunshineSyncAdapter", "NOOOOOOOOOOOOOOOOO, life is no good");
return;
}
Wearable.DataApi.putDataItem(mGoogleApiClient,request).setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
@Override
public void onResult(DataApi.DataItemResult dataItemResult) {
if (!dataItemResult.getStatus().isSuccess()) {
Log.v("MainActivity", "Something went wrong, watch was not notified");
} else {
Log.v("MainActivity", "Success, Watch Notified");
}
}
});
}
我首先在手机上运行我的应用程序,确保它正确运行并更新并将数据项发送到我的磨损设备。然后我运行我的wear模块并且什么也得不到,因为永远不会调用onDataChanged方法。
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
Log.v("SunshineWatchFace", "onDataChanged");
try{
for(DataEvent dataEvent: dataEvents){
if(dataEvent.getType() != DataEvent.TYPE_CHANGED){
continue;
}
DataItem dataItem = dataEvent.getDataItem();
if(dataItem.getUri().getPath().compareTo("weather_update") == 0){
DataMap dataMap = DataMapItem.fromDataItem(dataItem).getDataMap();
minTemp = dataMap.getString("min-temp");
maxTemp = dataMap.getString("max-temp");
weatherImage = dataMap.getInt("weather-image");
Log.v("SunshineWatchFace", minTemp);
Log.v("SunshineWatchFace", maxTemp);
}
}
dataEvents.release();
if(!isInAmbientMode()){
invalidate();
}
}catch (Exception e){
Log.v("SunshineWatchFace",e.getMessage());
}
}
非常感谢任何帮助
我花了很多时间试图解决这个问题。
答案 0 :(得分:1)
您必须在WatchFace类中创建一个BroadcastReceiver,并在registerReceiver()方法下注册它。
首先创建BroadcastReceiver,例如:
final BroadcastReceiver mWeatherReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get the data from the intent here
// and set the variables you're going to use in your onDraw method
}
};
然后注册接收器,例如:
private void registerReceiver() {
//This is where you have the default watch face receiver
// Register your new weather receiver
IntentFilter weatherFilter = new IntentFilter("ACTION_WEATHER_CHANGED");
SunshineWatchFace.this.registerReceiver(mWeatherReceiver, weatherFilter );
}
最后,在WatchListenerService的onDataChanged上,通过Intent将数据发送到BroadcastReceiver。例如:
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
// Check the data type
if (event.getType() == DataEvent.TYPE_CHANGED) {
// Get the data map
dataMap = DataMapItem.fromDataItem(event.getDataItem()).getDataMap();
Log.i(LOG_TAG, "DataMap received on watch: " + dataMap);
// Get the data map items
String sunshine_temperature_high = dataMap.getString("sunshine_temperature_high");
String sunshine_temperature_low = dataMap.getString("sunshine_temperature_low");
// Create the intent
Intent send_weather = new Intent("ACTION_WEATHER_CHANGED");
send_weather.putExtra("sunshine_temperature_high", sunshine_temperature_high);
send_weather.putExtra("sunshine_temperature_low", sunshine_temperature_low);
// Broadcast it
sendBroadcast(send_weather);
}
else if (event.getType() == DataEvent.TYPE_DELETED) {
// DataItem deleted
}
}
答案 1 :(得分:1)
在GitHub上向您发送了一个带有更正的拉取请求。 好像你只是忘了连接你的谷歌客户端:))
mGoogleApiClient.connect();
您的代码中的一些额外的小变化(一些重构)。
答案 2 :(得分:0)
您是否将CanvasWatchFaceService添加为数据更改事件的侦听器?在onConnected中,请确保您致电:
Wearable.DataApi.addListener(mGoogleApiClient, this);
答案 3 :(得分:0)
你应该经常检查:
我被GoUbiquitous困住了10天,直到找到最后一个: - \