14

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

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

并且不返回WALKINGRUNNING或者根本不返回ON_FOOT

我应该注意的 Activity Recognition API 是否有任何更改?

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

4

4 回答 4

13

WALKINGRUNNING活动作为列表 ( 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;
}

今天晚上我测试了上面的代码,无论是走路还是跑步,它似乎都做得很好。如果您没有明确过滤仅RUNNINGor WALKING,您可能会得到错误的结果。

以下是处理新活动结果的完整方法。我直接从示例应用程序中提取了这个,并且已经测试了几天,结果很好。

/**
 * 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";
    }
}
于 2014-07-23T03:46:07.317 回答
1

主要变化是 ON_FOOT 现在返回检测到的活动列表。现在改用 getMostProbableActivities()。

当 ON_foot 获得如下检测到的活动列表时,此解决方案会走路或跑步:

//Get the list from the result
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
ArrayList<DetectedActivity> activityList = new ArrayList<DetectedActivity>(result.getProbableActivities());

//Get the most probable activity
getMostProbableActivity(activityList);

现在传递您的列表以查找最可能的活动,如下所示:

private DetectedActivity getMostProbableActivity(List<DetectedActivity> detectedActivityList)
{
DetectedActivity result = null;

//Find the most probably activity in the list
for(DetectedActivity detectedActivity : detectedActivityList)
{
    if(detectedActivity.getType() != DetectedActivity.ON_FOOT)
    {
        if(result == null)
        {
            result = detectedActivity;
        }
        else
        {
            if(result.getConfidence() < detectedActivity.getConfidence())
            {
                result = detectedActivity;
            }
        }
    }
}

return result;

}

于 2014-07-23T20:24:21.800 回答
1

您可以尝试这个简单的“for 循环”来确保您的用户正在开车。

for (DetectedActivity detectedActivity : detectedActivityList)
        {
           {
              if(DetectedActivity == “In_Vehicle” && result.getConfidence()> 75)
                     {
                        // output = User is Driving;
                        // Perform task 
                     }
           }
        }

请记住,要让 Google Play 服务确保您的用户正在执行某项任务,置信度必须大于 75,只有这样您才能确定该任务已执行。或者,您可以尝试其中一些免费的 SDK,例如 Tranql、Neura 或 ContextHub,它们可以让您更好地了解用户的活动和位置。

于 2016-12-22T00:08:30.097 回答
0

我有 Google play Services 5.0.84,它适用于我的 Nexus 5。不知道你在说什么,所以它可能是你代码中的错误。

我的应用程序每分钟不断采样,并返回(大部分时间)正确的活动。开车/步行/倾斜/步行..一切都来了..

另外,如果你不使用getMostProbableActivity,那么你应该使用它!评论:在特定设备或某些供应商固件中确实可能会出现问题,但这不太可能。

于 2014-07-24T05:37:28.510 回答