How to use DataItem of Android Wear

前端 未结 1 747
夕颜
夕颜 2021-01-02 04:46

I want to sync preference between handhelds and wearables. I implement sample code on handheld app.

PutDataMapRequest dataMap = PutDataMapRequest.create("         


        
相关标签:
1条回答
  • 2021-01-02 05:13

    With that code, you are trying to create a second put request, not reading the previously stored data. That's why it's empty.

    The way to access previously stored data is with the DataApi methods. For example, you can get all stored data with Wearable.DataApi.getDataItems():

    PendingResult<DataItemBuffer> results = Wearable.DataApi.getDataItems(mGoogleApiClient);
    results.setResultCallback(new ResultCallback<DataItemBuffer>() {
        @Override
        public void onResult(DataItemBuffer dataItems) {
            if (dataItems.getCount() != 0) {
                DataMapItem dataMapItem = DataMapItem.fromDataItem(dataItems.get(0));
    
                // This should read the correct value.
                int value = dataMapItem.getDataMap().getInt(COUNT_KEY);
            }
    
            dataItems.release();
        }
    });
    

    I've used this and it works. However, I'm having a problem myself, as I don't know the Uri to access a specific data item with Wearable.DataApi.getDataItem(). So I posted this question. If you're just testing though, DataApi.getDataItems() should suffice.

    Another option is to use DataApi.addListener() to be notified of changes to the storage.

    0 讨论(0)
提交回复
热议问题