行为识别API [英] Activity Recognition API

查看:1060
本文介绍了行为识别API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何有最近谷歌的活动识别API的麻烦播放服务更新?

Anyone have trouble with the Activity Recognition API in the recent Google Play Services update?

我有一个应用程序来实现。这是工作完全正常的5.0更新。现在,它返回 IN_VEHICLE 当用户正在行走或坐不动。 :/

I have it implemented in an app. It was working perfectly fine before the 5.0 update. Now it returns IN_VEHICLE when the user is walking or sitting still. :/

和不返回步行运行 ON_FOOT 可言。

有没有任何改变活动识别API,我应该知道的?

Were there any changes to the Activity Recognition API I should be aware of?

让我知道如果你需要任何更多的细节。

Let me know if you need any more details.

推荐答案

步行运行活动进来作为<一个href="https://developer.android.com/reference/com/google/android/gms/location/ActivityRecognitionResult.html#getProbableActivities()">secondary在列表中的活动( ActivityRecognitionResult.getProbableActivities()),你会需要解析出来。

The WALKING and RUNNING activities come in as secondary activities in a list (ActivityRecognitionResult.getProbableActivities()), and you'll need to parse them out.

// Get the update
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);

// Get the most probable activity from the list of activities in the update
DetectedActivity mostProbableActivity = result.getMostProbableActivity();

// Get the type of activity
int activityType = mostProbableActivity.getType();

if (activityType == DetectedActivity.ON_FOOT) {
    DetectedActivity betterActivity = walkingOrRunning(result.getProbableActivities());
    if (null != betterActivity)
        mostProbableActivity = betterActivity;
}

private DetectedActivity walkingOrRunning(List<DetectedActivity> probableActivities) {
    DetectedActivity myActivity = null;
    int confidence = 0;
    for (DetectedActivity activity : probableActivities) {
        if (activity.getType() != DetectedActivity.RUNNING && activity.getType() != DetectedActivity.WALKING)
            continue;

        if (activity.getConfidence() > confidence)
            myActivity = activity;
    }

    return myActivity;
}

我测试C此日晚上述$ C $,无论是行走和奔跑,似乎做的相当好。如果你没有明确筛选仅运行步行,你可能会得到错误的结果。

I tested the above code this evening, both walking and running and it seemed to do fairly well. If you don't explicitly filter on only RUNNING or WALKING, you will likely get erroneous results.

下面是一个完整的方法来处理新的活动的结果。我把这个直出的示例应用程序,并一直在测试它的效果也不错了两天。

Below is a full method for handling new activity results. I pulled this straight out of the sample app, and have been testing it for a couple of days with good results.

/**
 * Called when a new activity detection update is available.
 */
@Override
protected void onHandleIntent(Intent intent) {
    Log.d(TAG, "onHandleIntent");

    // Get a handle to the repository
    mPrefs = getApplicationContext().getSharedPreferences(
            Constants.SHARED_PREFERENCES, Context.MODE_PRIVATE);

    // Get a date formatter, and catch errors in the returned timestamp
    try {
        mDateFormat = (SimpleDateFormat) DateFormat.getDateTimeInstance();
    } catch (Exception e) {
        Log.e(TAG, getString(R.string.date_format_error));
    }

    // Format the timestamp according to the pattern, then localize the pattern
    mDateFormat.applyPattern(DATE_FORMAT_PATTERN);
    mDateFormat.applyLocalizedPattern(mDateFormat.toLocalizedPattern());

    // If the intent contains an update
    if (ActivityRecognitionResult.hasResult(intent)) {

        // Get the update
        ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);

        // Log the update
        logActivityRecognitionResult(result);

        // Get the most probable activity from the list of activities in the update
        DetectedActivity mostProbableActivity = result.getMostProbableActivity();

        // Get the confidence percentage for the most probable activity
        int confidence = mostProbableActivity.getConfidence();

        // Get the type of activity
        int activityType = mostProbableActivity.getType();
        mostProbableActivity.getVersionCode();

        Log.d(TAG, "acitivty: " + getNameFromType(activityType));

        if (confidence >= 50) {
            String mode = getNameFromType(activityType);

            if (activityType == DetectedActivity.ON_FOOT) {
                DetectedActivity betterActivity = walkingOrRunning(result.getProbableActivities());

                if (null != betterActivity)
                    mode = getNameFromType(betterActivity.getType());
            }

            sendNotification(mode);
        }
    }
}

private DetectedActivity walkingOrRunning(List<DetectedActivity> probableActivities) {
    DetectedActivity myActivity = null;
    int confidence = 0;
    for (DetectedActivity activity : probableActivities) {
        if (activity.getType() != DetectedActivity.RUNNING && activity.getType() != DetectedActivity.WALKING)
            continue;

        if (activity.getConfidence() > confidence)
            myActivity = activity;
    }

    return myActivity;
}

/**
 * Map detected activity types to strings
 *
 * @param activityType The detected activity type
 * @return A user-readable name for the type
 */
private String getNameFromType(int activityType) {
    switch (activityType) {
        case DetectedActivity.IN_VEHICLE:
            return "in_vehicle";
        case DetectedActivity.ON_BICYCLE:
            return RIDE;
        case DetectedActivity.RUNNING:
            return RUN;
        case DetectedActivity.WALKING:
            return "walking";
        case DetectedActivity.ON_FOOT:
            return "on_foot";
        case DetectedActivity.STILL:
            return "still";
        case DetectedActivity.TILTING:
            return "tilting";
        default:
            return "unknown";
    }
}

这篇关于行为识别API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆