我的目标
我正在创建一个app / package,其中wear和掌上电脑能够在数据层上进行更改并收到有关此类更改的通知。我正在使用GoogleApiClient和DataApi.DataListener接口。
当前进展
截至目前,我的Wearable上的操作成功更新了数据层中的计数,然后通过显示的新计数检测到此更改并反映在手持设备中。
问题
检测只能以一种方式进行。虽然Wearable启动的更改成功更新了Handheld上的计数,但是在Wearable上未检测到手持设备发起的更改。
我尝试过什么
我已经看了StackExchange和Google几个小时,我还没有发现Wearable和Handheld都在更新数据层对象的情况。在大多数情况下,只有Handheld更新了一些东西和Wearable检测,反之亦然。单向通信对我来说很好。我需要双向。
手持代码片段
public class ListenActivity extends Activity implements
DataApi.DataListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
// ConnectionCallback methods, UI methods not included in snippet
private static final String COUNT_KEY = "com.example.count";
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listen);
handler.post(displayCounts);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
@Override
public void onDataChanged(DataEventBuffer dataEvents)
{
for (DataEvent event : dataEvents)
{
if (event.getType() == DataEvent.TYPE_CHANGED)
{
DataItem item = event.getDataItem();
if (item.getUri().getPath().compareTo("/count") == 0)
{
DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
count = dataMap.getInt(COUNT_KEY);
}
}
}
}
// A UI button calls this method to reset the count, but the Wearable isn't calling its onDataChanged() method.
public void resetCount(View view)
{
PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/count");
putDataMapReq.getDataMap().putInt(COUNT_KEY, 0);
PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult =
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
} }
可穿戴代码片段
public class WearListenActivity extends Activity implements
DataApi.DataListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
// ConnectionCallback methods, UI methods not included in snippet
private static final String COUNT_KEY = "com.example.count";
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDismissOverlay = (DismissOverlayView) findViewById(R.id.dismiss_overlay);
mDismissOverlay.showIntroIfNecessary();
mGestureDetector = new GestureDetectorCompat(this, new GestureListener());
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApiIfAvailable(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
// This method is never called on the Wearable.
// It isn't detecting the changes made by Handheld's resetCount() method?
@Override
public void onDataChanged(DataEventBuffer dataEvents)
{
for (DataEvent event : dataEvents)
{
if (event.getType() == DataEvent.TYPE_CHANGED)
{
DataItem item = event.getDataItem();
if (item.getUri().getPath().compareTo("/count") == 0)
{
DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
count = dataMap.getInt(COUNT_KEY);
}
}
}
}
// User taps the screen, this method is called. Works perfectly fine since handheld updates its counts.
private void updateDataLayer()
{
PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/count");
putDataMapReq.getDataMap().putInt(COUNT_KEY, count);
PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult =
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
} }
答案 0 :(得分:2)
这可能是因为您正在侦听Activity中的事件。 我使用WearableListenerService并且它在两种方式都适用。
您可以在此处找到示例:Handling Data Layer Events