如何为Datalayer API清空DataEventBuffer?

时间:2016-06-03 14:50:43

标签: android wear-os

我正在尝试创建一个应用程序来发送和接收来自磨损和手持设备的数据。我想清空dataeventbuffer,我尝试发布,但这没有改变。任何帮助都会很棒。

这是我的穿着代码:

       accounts.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("Pressed","Now");
            PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/requestAccounts");
            int count=0;
            putDataMapReq.setUrgent();
           // PutDataRequest putDataReq;
            PendingResult<DataApi.DataItemResult> pendingResult;
           // putDataMapReq.getDataMap().putInt("key", count+1);
            putDataMapReq.getDataMap().putLong("time", new Date().getTime());
            PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();

            pendingResult = Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);

        }
    });

  @Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
    Log.d("Inside","OndataChanged");

    Log.d("No: events",dataEventBuffer.getCount()+"");
    for (DataEvent event : dataEventBuffer) {
        if (event.getType() == DataEvent.TYPE_CHANGED) {
            DataItem item = event.getDataItem();
            Log.d("URI",item.getUri().getPath());
            if (item.getUri().getPath().compareTo("/sendAccountResponse") == 0) {
                DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
               // Log.d("Datamap",dataMap.toString());
                if(!dataMap.getString("accounts").equals("test")){
                    Log.d("accounts",dataMap.getString("accounts"));
                    //event.freeze();
                    dataEventBuffer.release();
                    Intent showAccounts=new Intent(this,AcoountActivity.class);
                    showAccounts.putExtra("accounts",dataMap.getString("accounts"));
                    startActivity(showAccounts);

                }
            }
        }

这是我在手持设备上的代码:

@Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
    Log.d("Inside","On datachanged");
    for (DataEvent event : dataEventBuffer) {
        if (event.getType() == DataEvent.TYPE_CHANGED) {
            DataItem item = event.getDataItem();
            Log.d("Path",item.getUri().getPath());
            if (item.getUri().getPath().compareTo("/requestAccounts") == 0) {
                DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
               // if(dataMap.getInt("key")==1){
                    Log.d("Inside","if key==1");
                    dataEventBuffer.release();
                    PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/sendAccountResponse");
                    int count=0;
                    putDataMapReq.setUrgent();
                    PutDataRequest putDataReq;
                    PendingResult<DataApi.DataItemResult> pendingResult;
                    putDataMapReq.getDataMap().putString("accounts", getIntent().getStringExtra("accounts"));
                    putDataMapReq.getDataMap().putLong("time", new Date().getTime());
                    putDataReq = putDataMapReq.asPutDataRequest();
                    Log.d("Over","here");
                    pendingResult =
                            Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
                    //event.freeze();


               // }
            }

每次我关闭磨损的应用程序并再次启动它,并检查日志,它进入for循环“sendAccountResponse”的次数继续增加1.例如,如果我第5次启动应用程序,对于同一会话,并按“帐户”按钮,流程将为SendAccountResponse输入ondatachanged五次,依此类推。如何清空Dataeventbuffer?

0 个答案:

没有答案