有人对最近的 Google Play 服务更新中的 Activity Recognition API 有问题吗?
我在一个应用程序中实现了它。在 5.0 更新之前它工作得很好。现在,当用户走路或坐着不动时,它会返回 IN_VEHICLE
。 :/
并且根本不返回 WALKING
、RUNNING
或 ON_FOOT
。
我应该注意 Activity Recognition API 的任何变化吗?
如果您需要更多详细信息,请告诉我。
最佳答案
WALKING
和RUNNING
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;
}
我今天晚上测试了上面的代码,无论是步行还是运行,它似乎都表现不错。如果您没有明确地仅过滤 RUNNING
或 WALKING
,您可能会得到错误的结果。
以下是处理新 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/