我想通过检查他们的步数增量是否 > 0 来检查某人是否正在使用 Google Fit API 行走。这似乎比使用 History API 获取每日步数并确定它是否比上一个查询增加更简单。
我按照此处的代码进行操作,但我没有进行位置跟踪,而是进行增量步数计数。但是,侦听器似乎没有检索到最新的增量步数。
这是我的代码:
public void buildFitnessClient()
{
if ( mClient == null && checkPermissions() )
{
mClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.SENSORS_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
.addConnectionCallbacks(
new GoogleApiClient.ConnectionCallbacks()
{
@Override
public void onConnected(Bundle bundle)
{
Log.d(TAG, "Connected!!!");
// Now you can make calls to the Fitness APIs.
findFitnessDataSources();
}
@Override
public void onConnectionSuspended(int i)
{
// If your connection to the sensor gets lost at some point,
// you'll be able to determine the reason and react to it here.
if ( i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST )
Log.d(TAG, "Connection lost. Cause: Network Lost.");
else if ( i
== GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED )
Log.d(TAG, "Connection lost. Reason: Service Disconnected");
}
}
)
.enableAutoManage(this, 0, new GoogleApiClient.OnConnectionFailedListener()
{
@Override
public void onConnectionFailed(ConnectionResult result)
{
Log.d(TAG, "Google Play services connection failed. Cause: " +
result.toString());
}
})
.build();
}
}
private void findFitnessDataSources()
{
// Note: Fitness.SensorsApi.findDataSources() requires the ACCESS_FINE_LOCATION permission.
Fitness.SensorsApi.findDataSources(mClient, new DataSourcesRequest.Builder()
// At least one datatype must be specified.
.setDataTypes(DataType.TYPE_STEP_COUNT_DELTA)
// Can specify whether data type is raw or derived.
.setDataSourceTypes(DataSource.TYPE_DERIVED)
.build())
.setResultCallback(new ResultCallback<DataSourcesResult>()
{
@Override
public void onResult(DataSourcesResult dataSourcesResult)
{
Log.d(TAG, "Result: " + dataSourcesResult.getStatus().toString());
for ( DataSource dataSource : dataSourcesResult.getDataSources() )
{
Log.d(TAG, "Data source found: " + dataSource.toString());
Log.d(TAG, "Data Source type: " + dataSource.getDataType().getName());
//Let's register a listener to receive Activity data!
if ( dataSource.getDataType().equals(DataType.TYPE_STEP_COUNT_DELTA)
&& mListener == null )
{
Log.d(TAG, "Data source for STEP_COUNT_DELTA found! Registering.");
registerFitnessDataListener(dataSource,
DataType.TYPE_STEP_COUNT_DELTA);
}
}
}
});
}
private void registerFitnessDataListener(DataSource dataSource, DataType typeStep)
{
mListener = new OnDataPointListener()
{
@Override
public void onDataPoint(DataPoint dataPoint)
{
Log.d(TAG, "Detected onDataPoint");
for ( Field field : dataPoint.getDataType().getFields() )
{
Value val = dataPoint.getValue(field);
Log.d(TAG, "Detected DataPoint field: " + field.getName());
Log.d(TAG, "Detected DataPoint value stepCount: " + val);
TransientValues.setStepCountDelta(val.asInt());
}
}
};
Fitness.SensorsApi.add(
mClient,
new SensorRequest.Builder()
.setDataSource(dataSource) // Optional but recommended for custom data sets.
.setDataType(typeStep) // Can't be omitted.
.setSamplingRate(1L, TimeUnit.SECONDS)
.build(),
mListener)
.setResultCallback(new ResultCallback<Status>()
{
@Override
public void onResult(Status status)
{
if ( status.isSuccess() ) Log.d(TAG, "Listener registered!");
else Log.d(TAG, "Listener not registered.");
}
});
}
onDataPoint 方法没有被调用,mListener = onDataPointListener(){...} -> registerFitnessDataListener(...)
我不知道为什么。