永远不会调用 OnDataChanged
OnDataChanged is never called
我正在尝试数据项 APi,这是我的代码
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle connectionHint) {
Log.d("Inside", "onConnected: " + connectionHint);
Toast.makeText(getApplicationContext(),"Inside On connected",Toast.LENGTH_SHORT).show();
// Now you can use the Data Layer API
//Creating Dataitem
PutDataMapRequest dataMapRequest = PutDataMapRequest.create("/count");
dataMapRequest.setUrgent();
DataMap datamap=dataMapRequest.getDataMap();
PutDataRequest putDataRequest=dataMapRequest.asPutDataRequest();
datamap.putString("key", "Value");
PendingResult<DataApi.DataItemResult> pendingResult =
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataRequest);
}
@Override
public void onConnectionSuspended(int cause) {
Log.d("Inside", "onConnectionSuspended: " + cause);
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.d("Inside", "onConnectionFailed: " + result);
}
})
// Request access only to the Wearable API
.addApiIfAvailable(Wearable.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
Log.d("Inside","onDataChanged");
}
我错过了什么触发数据更改?我阅读了 this and this 但我仍然不清楚我应该更改什么以输入已更改的数据
如果您使用的是DataApi.DataListener,那么您必须在GoogleApiClient 连接后添加Listener。像这样的东西:Wearable.DataApi.addListener(mGoogleApiClient, this)
在你的 GoogleApiClient.ConnectionCallbacks
.
里面
如果您使用的是 WearableListenerService,请确保您在 receiving/listening 端的 AndroidManifest.xml 中有类似的代码,如下所示:
<service android:name=".YourCustomWearableListenerService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.DATA_CHANGED"/>
<data android:scheme="wear"
android:host="*"
<!-- I believe pathPrefix is optional, removing it will make the service listener to all data change events -->
android:pathPrefix="/YourDataMapPathPrefix"/>
</intent-filter>
</service>
另请记住,onDataChanged
只有在数据实际更改时才会被调用。假设旧数据是("Person","Bob"),你用相同的数据("Person","Bob")替换它,那么onDataChanged
将得不到触发,因为数据实际上没有改变。但是如果你把 ("Person", "Bob2") 改为 onDataChanged
就会被触发。
我正在尝试数据项 APi,这是我的代码
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle connectionHint) {
Log.d("Inside", "onConnected: " + connectionHint);
Toast.makeText(getApplicationContext(),"Inside On connected",Toast.LENGTH_SHORT).show();
// Now you can use the Data Layer API
//Creating Dataitem
PutDataMapRequest dataMapRequest = PutDataMapRequest.create("/count");
dataMapRequest.setUrgent();
DataMap datamap=dataMapRequest.getDataMap();
PutDataRequest putDataRequest=dataMapRequest.asPutDataRequest();
datamap.putString("key", "Value");
PendingResult<DataApi.DataItemResult> pendingResult =
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataRequest);
}
@Override
public void onConnectionSuspended(int cause) {
Log.d("Inside", "onConnectionSuspended: " + cause);
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.d("Inside", "onConnectionFailed: " + result);
}
})
// Request access only to the Wearable API
.addApiIfAvailable(Wearable.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
Log.d("Inside","onDataChanged");
}
我错过了什么触发数据更改?我阅读了 this and this 但我仍然不清楚我应该更改什么以输入已更改的数据
如果您使用的是DataApi.DataListener,那么您必须在GoogleApiClient 连接后添加Listener。像这样的东西:Wearable.DataApi.addListener(mGoogleApiClient, this)
在你的 GoogleApiClient.ConnectionCallbacks
.
如果您使用的是 WearableListenerService,请确保您在 receiving/listening 端的 AndroidManifest.xml 中有类似的代码,如下所示:
<service android:name=".YourCustomWearableListenerService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.DATA_CHANGED"/>
<data android:scheme="wear"
android:host="*"
<!-- I believe pathPrefix is optional, removing it will make the service listener to all data change events -->
android:pathPrefix="/YourDataMapPathPrefix"/>
</intent-filter>
</service>
另请记住,onDataChanged
只有在数据实际更改时才会被调用。假设旧数据是("Person","Bob"),你用相同的数据("Person","Bob")替换它,那么onDataChanged
将得不到触发,因为数据实际上没有改变。但是如果你把 ("Person", "Bob2") 改为 onDataChanged
就会被触发。