OnDataChanged 永远不会被调用 [英] OnDataChanged is never called
问题描述
我正在尝试数据项 APi,这是我的代码
I am trying the dataitem APi and here is my code
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和this 但我仍然不清楚我应该更改什么才能输入更改的数据
What am I missing out to trigger on data changed?. I read this and this but I am still unclear what am I supposed to change to enter on data changed
推荐答案
如果你使用的是DataApi.DataListener,那么你必须在连接GoogleApiClient后添加Listener.类似于:Wearable.DataApi.addListener(mGoogleApiClient, this)
在您的 GoogleApiClient.ConnectionCallbacks
中.
If you are using DataApi.DataListener, then you have to addListener after GoogleApiClient connected. Something like: Wearable.DataApi.addListener(mGoogleApiClient, this)
inside your GoogleApiClient.ConnectionCallbacks
.
如果您使用的是 WearableListenerService,请确保您的 AndroidManifest.xml 中接收/侦听端具有类似的代码,如下所示:
If you are using WearableListenerService make sure you have similar code in your AndroidManifest.xml on the receiving/listening side as shown below:
<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
会被触发.
Also keep in mind that onDataChanged
will get call only if the data is ACTUALLY changed. Let's said that the old data is ("Person", "Bob") and you replace it with same data ("Person", "Bob"), then onDataChanged
will not get triggered since the data does not change in fact. But onDataChanged
will get triggered if you put ("Person", "Bob2") instead.
这篇关于OnDataChanged 永远不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!