我正在尝试使用dataitem 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");
}
答案 0 :(得分:4)
如果您使用的是DataApi.DataListener,则必须在连接GoogleApiClient后添加addListener。类似于: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
。让我们说旧的数据是(&#34; Person&#34;,&#34; Bob&#34;)并用相同的数据替换它(&#34; Person&#34;,&#34; ; Bob&#34;),然后onDataChanged
将不会被触发,因为数据实际上没有改变。但是,如果您将(&#34; Person&#34;,&#34; Bob2&#34;)改为使用,onDataChanged
将被触发。