1

我想检测我是否正在使用 Google 的 Play Service Api Client 进行活动识别。我已经在互联网上阅读了几篇关于这个主题的帖子(包括堆栈溢出),但我仍然无法理解如何使用 API。

我想知道检测我是否走路的最简单的代码是什么。我看到了这个网页:https ://tsicilian.wordpress.com/2013/09/23/android-activity-recognition/

用这些代码

 * Called when a new activity detection update is available.
 */
@Override
protected void onHandleIntent(Intent intent) {
     //...
      // If the intent contains an update
    if (ActivityRecognitionResult.hasResult(intent)) {
        // Get the update
        ActivityRecognitionResult result = 
          ActivityRecognitionResult.extractResult(intent);

         DetectedActivity mostProbableActivity 
                = result.getMostProbableActivity();

         // Get the confidence % (probability)
        int confidence = mostProbableActivity.getConfidence();

        // Get the type 
        int activityType = mostProbableActivity.getType();
       /* types:
        * DetectedActivity.IN_VEHICLE
        * DetectedActivity.ON_BICYCLE
        * DetectedActivity.ON_FOOT
        * DetectedActivity.STILL
        * DetectedActivity.UNKNOWN
        * DetectedActivity.TILTING
        */
        // process
    }

我只想要 getMostProbableActivity() 的结果,但我不知道如何得到它。这些代码应该放在哪里,除了这些代码我还需要做什么(例如在此之前我需要初始化什么,我需要实现其他类吗?这些代码在mainActivity中吗?)

我无法理解 Google 文档和网络上的任何教程,因为我无法理解它们(可能是因为它们的级别太高了)。

我真的很感激这个问题的答案。谢谢你。

4

1 回答 1

4

实际上谷歌有一个示例应用程序https://github.com/googlesamples/android-play-location/tree/master/ActivityRecognition

您需要请求ActivityUpdates,例如,从您的活动中

googleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(ActivityRecognition.API)
        .build();

googleApiClient.connect();

Intent intent = new Intent(this, YourService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
            googleApiClient,
            1000 /* detection interval */,
            pendingIntent);
于 2015-05-15T06:12:00.280 回答