未调用 Android Wear onDataChanged [英] Android Wear onDataChanged isn't called
问题描述
我下载了一个现成的手机应用程序,并尝试为我的 Android Wear 项目实现一个表盘.目标是将温度信息从手机传递到穿戴设备并让它更新表盘.
I downloaded a ready phone app and trying to implement a watchface for my android wear project. The goal is to pass temperature information from phone to the wear device and have it update the watchface.
我试过:1. 相同的包名2.相同的应用程序ID3. 相同的依赖版本4.相同的权限5. 在我的手机端,我确信我的数据每次都不同(因此必须进行更新)
I have tried: 1. same package name 2. same applicationId 3. same dependencies versions 4. same permissions 5. on my phone side I know for sure that my data is different every time (so the update has to happen)
我还使用数据项,因此我的数据必须同步.
I also use data items so my data has to get synced.
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");
}
}
});
}
我首先在手机上运行我的应用程序,确保它正确运行,并且它已更新并将数据项发送到我的穿戴设备.然后我运行我的磨损模块并没有收到任何信息,因为从未调用过 onDataChanged 方法.
I first run my app on the phone make sure it runs correctly and that it updated and sends data items to my wear device. Then I run my wear module and receive nothing because onDataChanged method is never being invoked.
@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());
}
}
非常感谢您的帮助
我花了很多时间试图解决这个问题.
I have spent a lot of time trying to resolve this problem.
推荐答案
在 GitHub 上向您发送了包含更正的拉取请求.好像你只是忘记连接你的谷歌客户端:)
Sent you a pull request on GitHub with corrections. Seems like you just forget to connect your google client :)
mGoogleApiClient.connect();
还有一些额外的小改动(一些重构).
And some additional small changes in your code (some refactoring).
这篇关于未调用 Android Wear onDataChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!