简体   繁体   English

如何使用 Google Fit API 的 RecordingClient 后台收集 DataType.TYPE_LOCATION_SAMPLE?

[英]How to use Google Fit API's RecordingClient for background collection of DataType.TYPE_LOCATION_SAMPLE?

I proceeded as per Google Fit examples and subscribed to the RecordingClient for DataType.TYPE_LOCATION_SAMPLE with the following code (after requesting all needed permissions):我按照Google Fit 示例进行操作,并使用以下代码订阅了DataType.TYPE_LOCATION_SAMPLE的 RecordingClient(在请求了所有需要的权限之后):

Fitness.getRecordingClient(this, GoogleSignIn.getLastSignedInAccount(this))
    .subscribe(DataType.TYPE_LOCATION_SAMPLE)
    .addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Log.d(LOG_TAG, "Successfully subscribed!");
        }
    })
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.d(LOG_TAG, "There was a problem subscribing.");
        }
    });

On the Logcat, I receive the messagge Successfully subscribed!在 Logcat 上,我收到消息已Successfully subscribed! , so everything seems to be OK. ,所以似乎一切正常。 In addition, if I run the following code, to double check:此外,如果我运行以下代码,请仔细检查:

Fitness.getRecordingClient(MainActivity.this, GoogleSignIn.getLastSignedInAccount(MainActivity.this))
    .listSubscriptions()
    .addOnSuccessListener(new OnSuccessListener<List<Subscription>>() {
        @Override
        public void onSuccess(List<Subscription> subscriptions) {
            for (Subscription sc : subscriptions) {
                Log.d(LOG_TAG, "Active subscription for data type: " + sc.getDataType().getName());
            }
        }
    });

I receive the output:我收到输出:

D/mygpsapp.log: Active subscription for data type: com.google.location.sample

confirming that the subscription is indeed active.确认订阅确实有效。 However, if (after moving around for one hour, for testing) I try to read data back from the HistoryClient, with the following code:但是,如果(移动一小时后,为了测试)我尝试从 HistoryClient 读回数据,代码如下:

DataReadRequest readRequest = new DataReadRequest.Builder()
    .read(DataType.TYPE_LOCATION_SAMPLE)
    .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
    .build();

Fitness.getHistoryClient(MainActivity.this, GoogleSignIn.getLastSignedInAccount(MainActivity.this))
    .readData(readRequest)
    .addOnSuccessListener(new OnSuccessListener<DataReadResponse>() {
        @Override
        public void onSuccess(DataReadResponse dataReadResponse) {
        Log.d(LOG_TAG, "HistoryClient.readData: onSuccess()");
        DataSet dataSet;

        List<DataSet> dataSets = dataReadResponse.getDataSets();
        Log.d(LOG_TAG, "# of DataSets: " + dataSets.size());
        dataSet = dataReadResponse.getDataSet(DataType.TYPE_LOCATION_SAMPLE);
            Log.d(LOG_TAG, "DataSet: " + dataSet);
            Log.d(LOG_TAG, "DataSource: " + dataSet.getDataSource());
            Log.d(LOG_TAG, "DataType: " + dataSet.getDataType());

        int count = 0;
        for (DataPoint dp : dataSet.getDataPoints()) {
            Log.d(LOG_TAG, "Data point #" + count++);
            Log.d(LOG_TAG, "\tType: " + dp.getDataType().getName());
            Log.d(LOG_TAG, "\tStart: " + dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
            Log.d(LOG_TAG, "\tEnd: " + dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS)));
            for (Field field : dp.getDataType().getFields()) {
                Log.d(LOG_TAG, "\tField: " + field.getName() + ", Value: " + dp.getValue(field));
            }
            Log.d(LOG_TAG, "\tSource: " + dp.getOriginalDataSource().getAppPackageName());
        }
        }
    })
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
        Log.e(LOG_TAG, "HistoryClient.readData: onFailure()", e);
        }
    });

I do get a relevant DataSet , but it is always empty:我确实得到了一个相关的DataSet ,但它总是空的:

D/mygpsapp.log: # of DataSets: 1
D/mygpsapp.log: DataSet: DataSet{d:location.sample:gms:default_location_samples []}
D/mygpsapp.log: DataSource: DataSource{derived:Application{com.google.android.gms}:default_location_samples:DataType{com.google.location.sample[latitude(f), longitude(f), accuracy(f), altitude(f)]}}
D/mygpsapp.log: DataType: DataType{com.google.location.sample[latitude(f), longitude(f), accuracy(f), altitude(f)]}

I acknowledge that the Google Fit API documentationreports the following :我承认 Google Fit API 文档报告了以下内容

Exceptions: Whilst any application can write data to a public data type, there are certain data types which can only be read by the application that wrote the data:例外:虽然任何应用程序都可以将数据写入公共数据类型,但某些数据类型只能由写入数据的应用程序读取:

com.google.location.sample, The user's current location. com.google.location.sample,用户的当前位置。

but I do try to read back the data with the same application .但我确实尝试使用相同的应用程序读回数据。 What makes me suspicious is that the previous Logcat seems to indicate that the application that wrote the data is com.google.android.gms (is it the RecordingClient?) not mine, possibly explaining why the DataSet returns empty.让我怀疑的是,之前的 Logcat 似乎表明写入数据的应用程序是com.google.android.gms (它是 RecordingClient?)不是我的,这可能解释了为什么 DataSet 返回空。 But if this is the case, how is my application supposed to read back the data that itself asked ( succesfully! ) to record?但如果是这种情况,我的应用程序应该如何读回本身要求(成功! )记录的数据?

Google Fit does not support reading location data which has been gathered via the Recording API via the History API. Google Fit 不支持通过 History API 读取通过 Recording API 收集的位置数据。

We'll clarify this in the documentation.我们将在文档中阐明这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Google定位服务API甚至在后台获取位置更新? - How to use Google Location service API to get location updates even in background? 如何使用Google Fit的API获取用户当前的速度? - How to get the user's current speed using Google Fit's API? Google Fit:什么是Google Fit的DataType Floor。 - Google Fit: What is DataType Floor of Google Fit. 如何在后台接收位置更新? (API 26) - How to receive location updates in background? (API 26) 如何在Java类中使用Google Page Speed API? 请提供相同的任何示例示例 - How to use google page speed API in java class ? Please provide any sample example for the same 如何使用适用于 Android 的 Google Fit API 查找步骤? - How to find steps with Google Fit API for Android? 如何在Google Analytic Java API中使用“购物产品类型”作为维度 - How to use “Shopping Product Type” as dimension in Google Analytic Java api 更改Google Maps API的“我的位置”按钮的位置 - Change position of Google Maps API's “My location” button 我的示例应用程序使用了多少SDK的API - How much of the API of an SDK does my sample application use Flink Java API - Pojo 类型到元组数据类型 - Flink Java API - Pojo Type to Tuple Datatype
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM