Android 获取每日步数
时间: 2023-08-18 22:15:08 浏览: 242
要获取 Android 设备的每日步数,可以使用 Google Fit API。以下是获取步数的简单示例代码:
1. 在 build.gradle 文件中添加以下依赖项:
```
dependencies {
implementation 'com.google.android.gms:play-services-fitness:20.0.0'
}
```
2. 在 AndroidManifest.xml 文件中添加以下权限:
```
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
```
3. 在 MainActivity.java 文件中添加以下代码:
```
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.fitness.Fitness;
import com.google.android.gms.fitness.data.DataSet;
import com.google.android.gms.fitness.request.DataReadRequest;
import com.google.android.gms.fitness.result.DataReadResult;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mClient = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create a GoogleApiClient instance
mClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.HISTORY_API)
.addScope(Fitness.SCOPE_ACTIVITY_READ)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
// Connect to the Google Fit API
mClient.connect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
// Get the current date
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
long startTime = cal.getTimeInMillis();
// Create a data request for the step count
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(Fitness.HISTORY_API, Fitness.HistoryApi.AggregateRequest.STEP_COUNT_DELTA)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
// Retrieve the step count
Fitness.HistoryApi.readData(mClient, readRequest).setResultCallback(new ResultCallback<DataReadResult>() {
@Override
public void onResult(DataReadResult dataReadResult) {
DataSet dataSet = dataReadResult.getDataSet(Fitness.HistoryApi.AggregateRequest.STEP_COUNT_DELTA);
int totalSteps = 0;
for (com.google.android.gms.fitness.data.DataPoint dp : dataSet.getDataPoints()) {
for (com.google.android.gms.fitness.data.Field field : dp.getDataType().getFields()) {
int steps = dp.getValue(field).asInt();
totalSteps += steps;
}
}
Log.i("Step Count", "Total steps: " + totalSteps);
}
});
}
@Override
public void onConnectionSuspended(int i) {
Log.i("Google Fit", "Connection suspended");
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.i("Google Fit", "Connection failed");
}
@Override
protected void onDestroy() {
super.onDestroy();
mClient.disconnect();
}
}
```
这将获取当前日期的步数,并将其打印到 Logcat 中。您可以根据需要更改日期范围。注意,此代码需要用户授权才能访问步数数据,因此您需要在运行时请求相应的权限。
阅读全文