gpt4 book ai didi

android - 永远不会调用 OnDataChanged

转载 作者:搜寻专家 更新时间:2023-11-01 07:49:24 27 4
gpt4 key购买 nike

我正在尝试数据项 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");
}

我错过了什么来触发数据更改?。我读了thisthis但我仍然不清楚我应该更改什么以输入已更改的数据

最佳答案

如果您使用的是DataApi.DataListener,那么您必须在GoogleApiClient 连接后添加Listener。类似于:Wearable.DataApi.addListener(mGoogleApiClient, this) 在您的 GoogleApiClient.ConnectionCallbacks 中。

如果您使用的是 WearableListenerService,请确保您的 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 将被触发。

关于android - 永远不会调用 OnDataChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37268174/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com