gpt4 book ai didi

java - 如何使用适用于 Android 的 Google Fit API 查找步数?

转载 作者:行者123 更新时间:2023-11-30 10:50:55 26 4
gpt4 key购买 nike

我已经就此进行了几天的研究。我只需要在我的应用程序中有一个 Simple TextView 区域来显示今天的步骤。

我已经设法让身份验证与下面的代码一起工作。它弹出请求许可,并认为我选择了正确的。

但我不知道如何简单地获取步数信息。我希望这只是几行代码。任何帮助,将不胜感激。谢谢

编辑 1:我只需要获取步数。我可以稍后弄清楚如何显示它。我还有 Toasts 来帮助我弄清楚发生了什么。

private void buildFitnessClient() {
if (mClient == null) {
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.i(TAG, "Connected!!!");
// Now you can make calls to the Fitness APIs.
Toast.makeText(getBaseContext(), "Connected!", Toast.LENGTH_LONG).show();


}

@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.i(TAG, "Connection lost. Cause: Network Lost.");
Toast.makeText(getBaseContext(), "Connection lost. Cause: Network Lost.", Toast.LENGTH_LONG).show();

} else if (i
== GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
Log.i(TAG,
"Connection lost. Reason: Service Disconnected");
}
}
}
)
.enableAutoManage(this, 0, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "Google Play services connection failed. Cause: " +
result.toString());
Toast.makeText(getBaseContext(), "Google Play services connection failed. Cause: " +
result.toString(), Toast.LENGTH_LONG).show();

}
})
.build();
}
}

最佳答案

根据新的更新,GoogleApiClient 已被弃用。 HistoryApi 也已弃用。所以首先必须使用 GoogleSignInAccount 而不是 GoogleApiClient 并且还要使用 Google Fit 的 HistoryClient 而不是 HistoryApi。

设置健身选项

 val fitnessOptions: GoogleSignInOptionsExtension = FitnessOptions.builder()
.addDataType(TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ).build()

设置 Google 登录选项

val googleSignInOptions =
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.addExtension(fitnessOptions).requestEmail().build()
val googleSignInClient = GoogleSignIn.getClient(requireActivity(), googleSignInOptions)
val signIntent = googleSignInClient.signInIntent
startActivityForResult(signIntent, 0)

获取帐户

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 0 && resultCode == Activity.RESULT_OK) {
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
val account = task.getResult(ApiException::class.java)
if (account != null) {
getGoogleFitData(account)
}
}
}

现在获取步数数据

val response: Task<DataReadResponse> =
Fitness.getHistoryClient(mContext, mSignInAccount)
.readData(
DataReadRequest.Builder()
.read(TYPE_STEP_COUNT_DELTA)
.setTimeRange(
startTime,
endTime,
TimeUnit.MILLISECONDS
)
.build()
)

val readDataResult: DataReadResponse? = Tasks.await(response)
val dataSet: DataSet = readDataResult!!.getDataSet(TYPE_STEP_COUNT_DELTA)

var total = 0
if (!dataSet.isEmpty) {
val dataPoints = dataSet.dataPoints
if (dataPoints.size > 0) {
for (i in 0 until dataPoints.size) {
total += dataSet.dataPoints[i].getValue(Field.FIELD_STEPS).asInt()
}
Log.e(TAG, "Total Steps : $total")
}
}
  • 这里的 startTime 和 endTime 是任何日期和时间的时间轴。

谢谢。

关于java - 如何使用适用于 Android 的 Google Fit API 查找步数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34902230/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com