简体   繁体   English

活动识别API

[英]Activity Recognition API

Anyone have trouble with the Activity Recognition API in the recent Google Play Services update? 在最近的Google Play服务更新中,任何人都无法使用活动识别API吗?

I have it implemented in an app. 我在应用程序中实现了它。 It was working perfectly fine before the 5.0 update. 它在5.0更新之前完全正常工作。 Now it returns IN_VEHICLE when the user is walking or sitting still. 现在,当用户走路或坐着不IN_VEHICLE时,它会返回IN_VEHICLE :/ :/

And doesn't return WALKING , RUNNING or ON_FOOT at all. 而且根本不会返回WALKINGRUNNINGON_FOOT

Were there any changes to the Activity Recognition API I should be aware of? 我应该注意哪些活动识别API有任何变化?

Let me know if you need any more details. 如果您需要更多详细信息,请与我们联系。

The WALKING and RUNNING activities come in as secondary activities in a list ( ActivityRecognitionResult.getProbableActivities() ) , and you'll need to parse them out. 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;
}

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. 如果您没有明确过滤仅RUNNINGWALKING ,您可能会得到错误的结果。

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

The main change is that ON_FOOT now returns a list of Detected Activities. 主要变化是ON_FOOT现在返回检测到的活动列表。 Use getMostProbableActivities() instead now. 现在使用getMostProbableActivities()。

this solution gets walking or running when ON_foot get a list of Detected activities like this: 当ON_foot获取如下所示的Detected活动列表时,此解决方案会行走或运行:

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

Now pass your list to find the most probable activity like this: 现在通过您的列表来查找最可能的活动,如下所示:

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;

} }

You could try this simple 'for loop' to be sure that your user is driving. 您可以尝试这个简单的'for loop'来确保您的用户正在驾驶。

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

Remember, for Google Play services to be sure that your user is performing a certain task, the confidence level must be greater than 75, only then you can be certain that the task is performed. 请注意,对于Google Play服务,要确保您的用户正在执行某项任务,置信度必须大于75,然后才能确定该任务已执行。 Alternatively, you can try some of these free SDKs like Tranql, Neura or ContextHub which can give you better insights about your user's activities and locations. 或者,您可以尝试一些免费的SDK,如Tranql,Neura或ContextHub,它们可以让您更好地了解用户的活动和位置。

I have Google play Services 5.0.84 it works fine with my Nexus 5. don't know what you are talking about, so it's probably bug in your code. 我有谷歌播放服务5.0.84它与我的Nexus 5一起工作正常。不知道你在说什么,所以它可能是你的代码中的错误。

my application sampling constantly every minute, and returns (most of the time) the right activity. 我的应用程序每分钟不断采样,并返回(大部分时间)正确的活动。 driving/walking/tilting/on foot.. every thing comes.. 开车/步行/倾斜/徒步..每件事都来了......

also, If you are not using getMostProbableActivity , then you should use it! 另外,如果您没有使用getMostProbableActivity ,那么您应该使用它! comment: it might be indeed that in specific devices or some vendor firmwares things will break, but it's not likely. 评论:可能确实在特定设备或某些供应商固件中,事情会破裂,但不太可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM