Android 如何使用Google Play服务API客户端进行活动识别

Android 如何使用Google Play服务API客户端进行活动识别,android,google-play-services,activity-recognition,Android,Google Play Services,Activity Recognition,我想检测我是否正在使用Google的Play Service Api客户端进行活动识别。我在互联网上读过几篇关于这个主题的帖子(包括堆栈溢出),但我仍然不理解如何使用API 我想知道检测我是否在走路的最简单的代码是什么。我看到这个网页: 使用这些代码 * Called when a new activity detection update is available. */ @Override protected void onHandleIntent(Intent intent) {

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

我想知道检测我是否在走路的最简单的代码是什么。我看到这个网页:

使用这些代码

 * 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中吗?)

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


我真的很感谢你的回答。谢谢。

实际上,谷歌提供了一个示例应用程序

您需要请求活动更新,例如从您的活动

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

实际上,谷歌有一个示例应用程序

您需要请求活动更新,例如从您的活动

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

谢谢你的回复。谷歌的示例应用程序太难了,我不知道是怎么回事。我甚至不知道你的代码在做什么,特别是这三行:Intent Intent=newintent(this,YourService.class);PendingEvent PendingEvent=PendingEvent.getService(this,0,intent,PendingEvent.FLAG_UPDATE_CURRENT);ActivityRecognition.ActivityRecognitionApi.RequestActivityUpdate(GoogleAppClient,1000/*检测间隔*/,待定内容);您能解释一下吗?ActivityRecognition是异步完成的,您需要两个代码框架-一个用于请求,一个用于接收结果。我回答中的snnipet代码用于请求ActivityRecognition。您可以将此代码放在任何您想要请求ActivityRecognition的位置。e、 例如,在您的活动的创建过程中。接收结果(您的问题中的某些内容)的代码在您的服务类中是实时的。还要确保您在AndroidManifest.xml中声明了服务。谢谢您的回复。谷歌的示例应用程序太难了,我不知道是怎么回事。我甚至不知道你的代码在做什么,特别是这三行:Intent Intent=newintent(this,YourService.class);PendingEvent PendingEvent=PendingEvent.getService(this,0,intent,PendingEvent.FLAG_UPDATE_CURRENT);ActivityRecognition.ActivityRecognitionApi.RequestActivityUpdate(GoogleAppClient,1000/*检测间隔*/,待定内容);您能解释一下吗?ActivityRecognition是异步完成的,您需要两个代码框架-一个用于请求,一个用于接收结果。我回答中的snnipet代码用于请求ActivityRecognition。您可以将此代码放在任何您想要请求ActivityRecognition的位置。e、 例如,在您的活动的创建过程中。接收结果(您的问题中的某些内容)的代码在您的服务类中是实时的。还要确保在AndroidManifest.xml中声明服务