Activity Recognition API

前端 未结 4 708
醉酒成梦
醉酒成梦 2021-01-31 11:32

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

I have it implemented in an app. It was working perfectly fine before th

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-31 12:31

    The main change is that ON_FOOT now returns a list of Detected Activities. Use getMostProbableActivities() instead now.

    this solution gets walking or running when ON_foot get a list of Detected activities like this:

    //Get the list from the result
    ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
    ArrayList activityList = new ArrayList(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 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;
    

    }

提交回复
热议问题