OnDataChanged 未被调用

OnDataChanged is not called

我的 phone 和 Wear 设备之间的通信有问题。我决定将 wear 模块添加到我的应用程序中。 Wear app 只有一个 class (MainActivity)

package cz.johrusk.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.wearable.view.WatchViewStub;
import android.util.Log;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.wearable.DataApi;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.PutDataMapRequest;
import com.google.android.gms.wearable.PutDataRequest;
import com.google.android.gms.wearable.Wearable;


public class MainActivity extends Activity implements GoogleApiClient.OnConnectionFailedListener,DataApi.DataListener {

GoogleApiClient mGoogleApiClient;
private TextView mTextView;
static final String TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            mTextView = (TextView) stub.findViewById(R.id.text);
        }
    });
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle connectionHint) {
                    Log.d(TAG, "onConnected: " + connectionHint);
                    sendNumber(1);
                    Log.d(TAG,"BBBBBBBB");
                }
                @Override
                public void onConnectionSuspended(int cause) {
                    Log.d(TAG, "onConnectionSuspended: " + cause);
                }
            })
            .addOnConnectionFailedListener(this)
            .build();
    mGoogleApiClient.connect();
    Log.d(TAG,"mGoogleApiClient connected;");
}


@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Log.d(TAG,"FAILE" + connectionResult);
}
public void sendNumber(int number) {
    PutDataMapRequest putDataMapRequest = PutDataMapRequest.create("/number");

    putDataMapRequest.getDataMap().putInt("number",number);
    putDataMapRequest.getDataMap().putLong("Time",System.currentTimeMillis());
    PutDataRequest putDataReq = putDataMapRequest.asPutDataRequest();
    putDataReq.setUrgent();
    Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq)
            .setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
                @Override
                public void onResult(@NonNull DataApi.DataItemResult dataItemResult) {
                    if (!dataItemResult.getStatus().isSuccess()) {
                        Log.d(TAG,"Fail");
                    }
                    else{
                        Log.d(TAG,"Succes");
                    }
                }
            });
}
@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}
@Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
    Log.d(TAG,"TEST");
}

}

我想 "Wearable.DataApi.putDataItem" 应该在我的 phone 应用程序中调用 Wea​​rableListenerService。这是该服务:

package cz.johrusk.showsmscode.service;


import android.util.Log;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.DataMap;
import com.google.android.gms.wearable.DataMapItem;
import com.google.android.gms.wearable.Wearable;
import com.google.android.gms.wearable.WearableListenerService;


 public class WatchListener_service extends WearableListenerService {
@Override
public void onCreate() {
    super.onCreate();
   GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .build();
    mGoogleApiClient.connect();
}
@Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
    Log.d("prijato","number is: ");

    for (DataEvent dataEvent : dataEventBuffer) {
    if (dataEvent.getType() == DataEvent.TYPE_CHANGED) {
        DataMap dataMap = DataMapItem.fromDataItem(dataEvent.getDataItem()).getDataMap();
        String path = dataEvent.getDataItem().getUri().getPath();
        if (path.equals("/number")){
            int number = dataMap.getInt("numa");
            long time = dataMap.getInt("timestamp");
            Log.d("received","number is: " + number);

        }
    }
}
}

}

但是,WatchListener_service 中的 onDataChanged 方法没有被调用。 ResultCallbacks print "Succes" 中的 onResult 方法,因此 DataItem 似乎已正确发送。

我已经在 Stackoverlflow 上发现了很多类似的问题,所以我检查了所有这些:

  1. 两个模块具有相同的 applicationId
  2. 两个模块都使用'com.google.android.gms:play-services-wearable:9.0.0'
  3. SetUrgent 用于 putDataRequest,所以应该不会有任何延迟。
  4. WearableListenerService 已在清单中声明了正确的 Intent 过滤器:

    行动android:name="com.google.android.gms.wearable.DATA_CHANGED" 数据 android:scheme="wear" android:host="*"

phone 和 Wear 应用 运行 在物理设备上。我的问题是......我应该怎么做才能解决这个问题? 谢谢

检查两个应用程序是否具有相同的调试密钥。我遇到了一些问题,问题是两个应用程序(磨损和移动)的不同键。

PS:Android穿API太可笑了。我退出了 Wear 开发,因为 API 不能像他们说的那样工作是多么糟糕。