android - DataApi 是否可靠且几乎是实时的?极度延迟

标签 android wear-os google-api-client android-wear-data-api android-wear-notification

<分区>

我已经有一个 Android Wear 应用程序在市场上大约 10 个月了,但它停止工作了(不确定它何时停止工作)。

我正在尝试使用我在移动应用程序中记录的一些统计数据来更新我的可穿戴应用程序。正如我所说,这曾经工作得很好而且可靠。我什至为此添加了时间戳,因此它是唯一的,因为只有在数据发生变化时才会调用 onDataChanged。这保证了这一点。

我的问题

我可以通过调用 Wearable.DataApi.putDataItem 提交 PutDataMapRequest,并且在我的 ResultCallback 中获得成功的回调。

onDataChanged 通常在最后被调用,但延迟比以前太多了。 Whatsmore,我知道它设置正确,因为我有一个我启动的应用程序,其中我有一个 Activity 我去获取 DataItems 并且那里有积压.我在这个 Activity 中也遇到了与 onDataChanged 相同的问题。

我做了什么

  1. 确保 versionCode 和 versionName 在两个模块 build.gradle 文件中匹配。
  2. 确保 build.gradle 文件使用相同版本的支持和谷歌服务库。
  3. 确保两个类都连接到 GoogleApiClient
  4. 确保数据每次都不同,以强制它在服务中调用 onDataChanged。我不需要这个,只是为了让它正常工作。

我的代码

移动

if (mGoogleApiClient.isConnected()) {
            PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(path);
            putDataMapRequest.getDataMap().putString("content", content);
            putDataMapRequest.getDataMap().putString("title", title);
            putDataMapRequest.getDataMap().putLong("timestamp", DateTime.now().getMillis());
            PutDataRequest request = putDataMapRequest.asPutDataRequest();    

            Wearable.DataApi.putDataItem(mGoogleApiClient, request)
                    .setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
                        @Override
                        public void onResult(DataApi.DataItemResult dataItemResult) {
                            if (!dataItemResult.getStatus().isSuccess()) {
                                Log.e("WearDevice", "buildWatchOnlyNotification(): Failed to set the data, "
                                        + "status: " + dataItemResult.getStatus().getStatusCode());
                            } else {
                                    Log.d(LOG_TAG, "Notificaiton result callback returned successfully: "+dataItemResult.getDataItem().toString());//: "+node.getDisplayName());
                            }
                        }
                    });
}

穿着

我有一个扩展 WearableListenerService 的服务。这是在 list 中声明的​​。

<service android:name=".NotificationUpdateService">
            <intent-filter>
                <action android:name="com.google.android.gms.wearable.BIND_LISTENER"/>
            </intent-filter>
        </service>

这是我的服务。

@Override
    public void onCreate() {
        super.onCreate();

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        mGoogleApiClient.connect();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDataChanged(DataEventBuffer dataEvents) {
        if (MainActivity.DEBUGGING)
            Log.d(LOG_TAG, "onDataChanged");
        for (DataEvent dataEvent : dataEvents) {

            if (dataEvent.getType() == DataEvent.TYPE_CHANGED) {
                DataMap dataMap = DataMapItem.fromDataItem(dataEvent.getDataItem()).getDataMap();
                String content = dataMap.getString("content");
                String title = dataMap.getString("title");
                if ("/mydatapath".equals(dataEvent.getDataItem().getUri().getPath())) {
                    buildWearableOnlyNotification(title, content, false);
                }
            }
        }
    }

buildWearableOnlyNotification 方法创建并启动一个前台通知,并且在实际调用时效果很好。

最佳答案

您现在需要设置 urgent flag如果您希望立即同步您的数据,否则该框架将对这些调用进行批处理并在一次调用中完成,这意味着您最多可以看到 30 分钟的延迟;这是对最新的 Play 服务所做的更改,以便在不需要这种即时数据同步时节省电池电量。

关于android - DataApi 是否可靠且几乎是实时的?极度延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33900980/

相关文章:

google-api - 谷歌 OAuth2 API。检查用户有两个因素身份验证(不是 GSuite)

python - Google 云端硬盘 API 客户端 : How to get the list of files in a folder

ruby-on-rails - 解析来自 Google 大查询的响应

android - OpenCV Java : how to access coordinates returned by findNonZero()?

xml - 如何为 Android Wear 表盘使用 XML 布局?

java - 错误 "No resource identifier found for attribute ' rectLayout'”

android - 如何在 Android Wear 设备上运行 Xamarin.Forms 应用程序

android - 在 Android 自定义 View 中调用 obtainStyledAttributes() 的正确方法是什么

android - 用于 UI 测试的 Hilt 依赖注入(inject)说 "HiltAndroidRule"没有添加,但它是

android - 如何停止在android中显示黑屏?