android - Activity 识别 API

标签 android google-play-services activity-recognition

有人对最近的 Google Play 服务更新中的 Activity Recognition API 有问题吗?

我在一个应用程序中实现了它。在 5.0 更新之前它工作得很好。现在,当用户走路或坐着不动时,它会返回 IN_VEHICLE。 :/

并且根本不返回 WALKINGRUNNINGON_FOOT

我应该注意 Activity Recognition API 的任何变化吗?

如果您需要更多详细信息,请告诉我。

最佳答案

WALKINGRUNNING Activity 以secondary activities in a list (ActivityRecognitionResult.getProbableActivities()) 的形式出现。 ,您需要将它们解析出来。

// 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;
}

我今天晚上测试了上面的代码,无论是步行还是运行,它似乎都表现不错。如果您没有明确地仅过滤 RUNNINGWALKING,您可能会得到错误的结果。

以下是处理新 Activity 结果的完整方法。我直接从示例应用程序中提取了它,并且已经对其进行了几天的测试并取得了良好的效果。

/**
 * 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";
    }
}

关于android - Activity 识别 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24818517/

相关文章:

android - 将额外内容从 ANE 传递到 Flex 应用程序

android - 在 Android 中下载背景图片时需要帮助吗?

android - 如何在我的 Android 设备上禁用已安装的 Google Play 服务版本?

android - 将Android Wear的加速度计数据同步到手机或计算机

c# - Xamarin Android 构建失败 "Invalid value for outputAssembly"

java - 是否可以抽象出 XML 代码的重复部分?

android - google_play_service_lib.jar 丢失

android - findFragmentById(R.id.map) 在 Android L 中返回 null

android - Activity Recognition PendingIntent 停止在半夜被调用