Android没有为所有请求调用DataApi onDataChanged()

时间:2017-07-13 18:52:33

标签: android wear-os android-wear-data-api

我正在尝试使用DataApi.DataListeneronDataChanged将数据从手机发送到手表,但不会针对所有请求调用Wearable.DataApi.putDataItem(mGoogleApiClient, request)(只有部分已发送)从手机{{ 1}}虽然ResultCallback<DataApi.DataItemResult>()结果对所有请求都是成功的

确保数据始终不同(只有在数据发生变化时才调用onDataChanged())我正在为每个请求添加时间戳DataMap,如下所示:myRequestDataMap.putLong("timeStamp", System.currentTimeMillis())并立即调用setUrgent()

出于调试目的,我也会在请求后立即使用MessageApi发送消息并传递所有消息。

注意:

  • applicationId对于手机和观看应用来说都是相同的
  • play-services-wearable版本也相同且9.8.0
  • com.google.android.wearable:wearable版本为2.0.3
  • 使用LG G Watch和模拟器测试

编辑: 观看应用程序清单

<manifest
package="app.package"
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-feature
    android:name="android.hardware.type.watch"
    android:required="true"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.DeviceDefault.Light">

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>

    <meta-data
        android:name="com.google.android.wearable.standalone"
        android:value="false" />

    <activity
        android:name="app.package.Activity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

</application>

编辑2:

手机应用:

public class App extends Application implements MessageApi.MessageListener, GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {

@Override
public void onCreate() {
    super.onCreate();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Wearable.API)
            .build();
    mGoogleApiClient.connect();
}

@Override
public void onMessageReceived(final MessageEvent messageEvent) {
    //as response to data request
    ...
    //load data from DB
    ...
    while (!cursor.isAfterLast()) {
        ...

        PutDataMapRequest dataMapRequest = myObject.generateDataMap();
        dataMapRequest.getDataMap().putLong("timeStamp", System.currentTimeMillis());
        PutDataRequest request = dataMapRequest.asPutDataRequest();
        PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi
            .putDataItem(mGoogleApiClient, request);

        pendingResult.setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
            @Override
            public void onResult(final DataApi.DataItemResult result) {
                if (result.getStatus().isSuccess()) {
                    Log.d(TAG, "Success");
                } else {
                    Log.d(TAG, "Failed");
                }
            }
        });

        cursor.moveToNext();
    }

}   

}

穿上app:

public class WearActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener, DataApi.DataListener, NodeApi.NodeListener, 
    MessageApi.MessageListener {

@Override
public void onStart() {
    super.onStart();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mGoogleApiClient.connect();
}

@Override
public void onConnected(Bundle bundle) {
    Wearable.DataApi.addListener(mGoogleApiClient, this);
    Wearable.MessageApi.addListener(mGoogleApiClient, this);
    Wearable.NodeApi.addListener(mGoogleApiClient, this);
}

@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    final List<DataEvent> events = FreezableUtils.freezeIterable(dataEvents);
    for (DataEvent event : events) {
        if (event.getType() == DataEvent.TYPE_CHANGED) {
            DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
            ...
        }
    }
}

 @Override
public void onStop() {
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
        Wearable.DataApi.removeListener(mGoogleApiClient, this);
        Wearable.MessageApi.removeListener(mGoogleApiClient, this);
        Wearable.NodeApi.removeListener(mGoogleApiClient, this);
        mGoogleApiClient.disconnect();
    }

    super.onStop();
}

}

有人可以帮忙吗?

0 个答案:

没有答案