2014-07-18 20 views
13

Bất kỳ ai gặp sự cố với API nhận dạng hoạt động trong bản cập nhật Dịch vụ của Google Play gần đây?API nhận dạng hoạt động

Tôi đã triển khai ứng dụng trong ứng dụng. Nó hoạt động hoàn hảo trước bản cập nhật 5.0. Bây giờ nó trả về IN_VEHICLE khi người dùng đang đi bộ hoặc ngồi yên. :/

Và không trả lại WALKING, RUNNING hoặc ON_FOOT.

Có bất kỳ thay đổi nào đối với API nhận dạng hoạt động mà tôi cần biết không?

Hãy cho tôi biết nếu bạn cần thêm bất kỳ chi tiết nào.

+1

[ActivityRecognitionApi] (https://developers.google.com/android/reference/com/google/android/gms/location/ActivityRecognitionApi) bị khấu hao và Google khuyên sử dụng API không kết nối [ActivityRecognitionClient] (https: //developers.google.com/android/reference/com/google/android/gms/location/ActivityRecognitionClient.html) thay thế. – g2server

Trả lời

10

Các hoạt động WALKINGRUNNING có ở dạng secondary activities in a list (ActivityRecognitionResult.getProbableActivities()) và bạn sẽ cần phân tích chúng.

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

Tôi đã thử nghiệm mã trên vào tối nay, cả đi bộ và chạy và dường như hoạt động khá tốt. Nếu bạn không lọc chỉ một cách rõ ràng RUNNING hoặc WALKING, bạn có thể sẽ nhận được kết quả sai.

Dưới đây là phương pháp đầy đủ để xử lý các kết quả hoạt động mới. Tôi kéo thẳng ra khỏi ứng dụng mẫu, và đã thử nghiệm nó trong một vài ngày với kết quả tốt.

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

Cảm ơn bạn đã trả lời. Tôi không cần biết người dùng đang đi bộ hay đang chạy, tôi chỉ cần biết rằng người dùng đang đi bộ. Vì vậy, tôi sẽ để nó ở đó. Bây giờ tất cả những gì tôi cần làm là giải thích tại sao API trả về IN_VEHICLE khi người dùng đang đi bộ hoặc đang ngồi. – DrkStr

+0

Bạn có lọc dựa trên sự tự tin và sử dụng hoạt động có thể xảy ra nhất không? 'DetectedActivity mostProbableActivity = result.getMostProbableActivity();' và 'mostProbableActivity.getConfidence()> = 50' – emil10001

+0

Không, tôi chỉ sử dụng getMostProbableActivity(). Ill cho getConfidence() thử ngay hôm nay và cho bạn biết nó đi như thế nào. – DrkStr

1

Thay đổi chính là ON_FOOT hiện trả về danh sách các hoạt động đã phát hiện. Sử dụng getMostProbableActivities() thay vào đó ngay bây giờ.

giải pháp này được đi bộ hoặc chạy khi ON_foot có được một danh sách các hoạt động phát hiện như thế này:

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

Bây giờ vượt qua danh sách của bạn để tìm các hoạt động có thể xảy ra nhất như thế này:

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; 

}

0

Tôi có Dịch vụ Google Play 5.0.84 hoạt động tốt với Nexus 5. không biết bạn đang nói về cái gì, vì vậy nó có thể là lỗi trong mã của bạn.

lấy mẫu đơn của tôi liên tục mỗi phút và trả về (phần lớn thời gian) hoạt động phù hợp. lái xe/đi bộ/nghiêng/chân .. mọi thứ đều đến ..

cũng vậy, Nếu bạn không sử dụng getMostProbableActivity, thì bạn nên sử dụng nó! bình luận: nó có thể thực sự là trong các thiết bị cụ thể hoặc một số nhà cung cấp phần cứng sẽ phá vỡ, nhưng nó không có khả năng.

+0

Tôi đang sử dụng getMostProbableActivity(). Tôi chưa thử dùng getConfidence. – DrkStr

1

Bạn có thể thử ‘vòng lặp’ đơn giản này để đảm bảo rằng người dùng của bạn đang lái xe.

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

Hãy nhớ, để dịch vụ Google Play đảm bảo rằng người dùng của bạn đang thực hiện một tác vụ nhất định, mức độ tin cậy phải lớn hơn 75, chỉ sau đó bạn có thể chắc chắn rằng nhiệm vụ được thực hiện. Ngoài ra, bạn có thể thử một số SDK miễn phí như Tranql, Neura hoặc ContextHub có thể cung cấp cho bạn thông tin chi tiết hơn về hoạt động và vị trí của người dùng của bạn.

Các vấn đề liên quan