问题
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.
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");
}
}
});
}
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());
}
}
Would really appreciate any help
GitHub_Link
I have spent a lot of time trying to resolve this problem.
回答1:
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).
回答2:
You have to create a BroadcastReceiver in your WatchFace class, and register it under the registerReceiver() method.
Create the BroadcastReceiver first, for example:
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
}
};
Then register the receiver, for example:
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 );
}
Finally, on the onDataChanged of you WatchListenerService, send the data via Intent to the BroadcastReceiver. For example:
@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
}
}
回答3:
Did you add your CanvasWatchFaceService as listener to data changed events? In onConnected make sure you call:
Wearable.DataApi.addListener(mGoogleApiClient, this);
回答4:
Your should always check if:
- Your mobile is connected to the wear
- The package names are the same
- You are using the same Google Play service version on both apps
- You are signing both mobile and wear apps using the same keystore
I got stuck on GoUbiquitous for 10 days until I figure out the last one :-\
来源:https://stackoverflow.com/questions/36298330/android-wear-ondatachanged-isnt-called