gpt4 book ai didi

android - 从 Google Fit API Android 获取当前 Activity

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:27:45 24 4
gpt4 key购买 nike

我正在开发演示应用程序以使用 Google Fit 获取当前 Activity 样本。我可以正确地获得速度和距离。但它并没有经常返回“in_vehicle”或“骑自行车”状态,尽管我处于相同状态。找到相同的附加屏幕截图。我得到了 59.40KM/H(36.91 M/h) 的速度,当时它没有返回“in_vehicle” Activity 状态。

请提供相同的解决方案/反馈。

代码:

@Override
public void onDataPoint(DataPoint dataPoint) {
for (Field field : dataPoint.getDataType().getFields()) {
Value val = dataPoint.getValue(field);
if(field.getName().trim().toLowerCase().equals("activity"))
{
if(FitnessActivities.getName(Integer.parseInt(val.toString())).equals("biking"))
{
strState = "Cycling";
}
else if(FitnessActivities.getName(Integer.parseInt(val.toString())).equals("in_vehicle"))
{
strState = "Automotive";
}
else if(FitnessActivities.getName(Integer.parseInt(val.toString())).equals("walking"))
{
strState = "Walking";
}
else
{
strState = "Not Moving";
}
}
}
}

谢谢。

IMAGE

最佳答案

您可以在此处找到我创建的示例项目。

https://github.com/cyfung/ActivityRecognitionSample

重要说明:您可能无法像您请求的那样频繁地获取数据!

Beginning in API 21, activities may be received less frequently than the detectionIntervalMillis parameter if the device is in power save mode and the screen is off.

关键组件:

onCreate 中创建 GoogleApiClient

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

onStart 中连接和断开 api 客户端和 onStop按照 Google Api 文档中的建议。

  @Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
mStatusView.setText("connecting");
}

@Override
protected void onStop() {
super.onStop();
mGoogleApiClient.disconnect();
mStatusView.setText("disconnected");
}

开始 Activity 识别(不应在 Google Api 连接之前调用)。使用 PendingIntent.getService创建挂起的 Intent 作为回调。

final PendingResult<Status>
statusPendingResult =
ActivityRecognition.ActivityRecognitionApi
.requestActivityUpdates(mGoogleApiClient, DETECT_INTERVAL, PendingIntent
.getService(this, 0, new Intent(this, ActivityDetectionService.class),
PendingIntent.FLAG_UPDATE_CURRENT));
statusPendingResult.setResultCallback(this);

IntentService是建议回调的标准方法

public class ActivityDetectionService extends IntentService {

protected static final String TAG = "activityDetectionService";

public ActivityDetectionService() {
super(TAG);
}

@Override
protected void onHandleIntent(Intent intent) {
final ActivityRecognitionResult
activityRecognitionResult =
ActivityRecognitionResult.extractResult(intent);
if (activityRecognitionResult == null) {
return;
}

//process the result here, pass the data needed to the broadcast
// e.g. you may want to use activityRecognitionResult.getMostProbableActivity(); instead
final List<DetectedActivity>
probableActivities =
activityRecognitionResult.getProbableActivities();

sendBroadcast(MainActivity.newBroadcastIntent(probableActivities));
}
}

在 list 中注册服务。

    <service
android:name=".ActivityDetectionService"
android:exported="false">
</service>

要使用API​​,您还需要在 list 中添加以下内容。

<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

为了将数据返回到 Activity 中,我使用了在 onCreate 中创建的 BroadcastReceiver

mBroadcastReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
...
}
}

onResume中注册和注销和 onPause分别。

  @Override
protected void onResume() {
super.onResume();
registerReceiver(mBroadcastReceiver, newBroadcastIntentFilter());
}

@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mBroadcastReceiver);
}

关于android - 从 Google Fit API Android 获取当前 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32373465/

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