【问题标题】:Detect user activity (running, cycling, driving) using Android使用 Android 检测用户活动(跑步、骑自行车、驾驶)
【发布时间】:2015-01-19 10:39:47
【问题描述】:

使用我的 Android 设备,我如何检测用户是在步行、骑自行车还是开车? 我检查了Google Fit app. 它区分了跑步、骑自行车和驾驶。我不知道应该使用什么算法来区分这些活动。

我知道我必须使用加速度计传感器。但我仍然无法区分这些活动。

【问题讨论】:

    标签: android accelerometer android-sensors gyroscope


    【解决方案1】:

    这个问题很老了,但是由于那里有新技术,我认为值得一提,如果有人仍然遇到这个问题。

    我可以想出 3 个选项:

    1. 您可以实现自己的检测步行、驾驶、骑自行车的技术 - 使用 Activity recognitionreceiving location updates,虽然 我建议不要这样做,但不要重新发明轮子,有好的 api 已经开发出来了,现在是 2016 年了。
    2. 您可以使用Neura 的免费 SDK,它可以在您的用户开始/结束驾驶、开始/结束步行、开始/结束跑步、read more of the events you can get from Neura 时向您发送事件。

      查看git project:基本上,该项目包含 Nuera 可以检测到的所有事件。把这个项目变成你自己的很容易。

      我强烈推荐使用这个 Neura sdk 选项。

    3. 您可以使用谷歌的FenceApi 来声明围栏。 例如,这是一个检测驾驶围栏的代码。

      虽然这种方法看起来不错,但我面临这样一个事实,即这个 api 有时没有告诉我事件发生的时间,有时在我开始走路/跑步后,当 api 告诉我这一点时,它需要很长时间事件。

      一个。包含对您应用的 build.gradle 文件的依赖项:

         compile 'com.google.android.gms:play-services-location:+'
      
         compile 'com.google.android.gms:play-services-contextmanager:+'
      

      b.清单定义:

      <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
      <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
      
      <application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:supportsRtl="true"
          android:theme="@style/AppTheme" >
      
          <meta-data
              android:name="com.google.android.awareness.API_KEY"
              android:value="PUT_YOUR_AWARENESS_KEY_HERE" />
      
          <activity android:name=".MainActivity" >
              <intent-filter>
                  <action android:name="android.intent.action.MAIN" />
      
                  <category android:name="android.intent.category.LAUNCHER" />
              </intent-filter>
          </activity>
      </application>
      

      PUT_YOUR_AWARENESS_KEY_HERE :您需要生成一个密钥here

      c。您的 MainActivity 类 - 附加到代码的解释:

      public class MainActivity extends Activity {
      
          private GoogleApiClient mGoogleApiClient;
          private PendingIntent mPendingIntent;
          private FenceReceiver mFenceReceiver;
      
          // The intent action which will be fired when your fence is triggered.
          private final String FENCE_RECEIVER_ACTION = BuildConfig.APPLICATION_ID + "FENCE_RECEIVER_ACTION";
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Awareness.API).build();
              mGoogleApiClient.connect();
              // Set up the PendingIntent that will be fired when the fence is triggered.
              mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(FENCE_RECEIVER_ACTION), 0);
              // The broadcast receiver that will receive intents when a fence is triggered.
              mFenceReceiver = new FenceReceiver();
              registerReceiver(mFenceReceiver, new IntentFilter(FENCE_RECEIVER_ACTION));
              createFence(DetectedActivityFence.IN_VEHICLE, "InVehicleFence");
          }
      
          @Override
          public void onDestroy() {
              try {
                  unregisterReceiver(mFenceReceiver); //Don't forget to unregister the receiver
              } catch (Exception e) {
                  e.printStackTrace();
              }
              super.onDestroy();
          }
      
          private void createFence(int detectedActivityFence, final String fenceKey) {
              AwarenessFence fence = DetectedActivityFence.during(detectedActivityFence);
              // Register the fence to receive callbacks.
              Awareness.FenceApi.updateFences(
                      mGoogleApiClient, new FenceUpdateRequest.Builder().addFence(fenceKey, fence, mPendingIntent)
                              .build()).setResultCallback(new ResultCallback<Status>() {
                  @Override
                  public void onResult(@NonNull Status status) {
                      if (status.isSuccess()) {
                          Log.i(getClass().getSimpleName(), "Successfully registered.");
                      } else {
                          Log.e(getClass().getSimpleName(), "Could not be registered: " + status);
                      }
                  }
              });
          }
      
          // Handle the callback on the Intent.
          public class FenceReceiver extends BroadcastReceiver {
              @Override
              public void onReceive(Context context, Intent intent) {
                  FenceState fenceState = FenceState.extract(intent);
                  switch (fenceState.getCurrentState()) {
                      case FenceState.TRUE:
                          Log.i(fenceState.getFenceKey(), "Active");
                          break;
                      case FenceState.FALSE:
                          Log.i(fenceState.getFenceKey(), "Not Active");
                          break;
                  }
              }
          }
      }
      

      此示例仅用于检测驾驶状态,但是,您可以使用其他活动方法调用“createFence”,例如:

      createFence(DetectedActivityFence.TILTING, "TiltingFence");
      createFence(DetectedActivityFence.WALKING, "WalkingFence");
      createFence(DetectedActivityFence.ON_FOOT, "OnFootFence");
      createFence(DetectedActivityFence.RUNNING, "RunningFence");
      

    【讨论】:

    • 我想知道您为什么建议使用 Neura 而不是“ActivityRecognition”API。 Neura SDK 是否使用一些不同的 API 或方法来检测活动?好点了吗?
    【解决方案2】:

    您可以为此使用 GooglePlayServices。

    它为ActivityRecognition提供了特殊的api,它返回每个用户活动的置信度。

    https://developer.android.com/guide/topics/location/transitions

    http://developer.android.com/training/location/activity-recognition.html

    【讨论】:

    • 听起来不错。是否需要任何最低 API 级别,或者只需要播放服务库即可完成工作?
    • 仅供参考,Google 有一个示例项目来说明如何使用此 API。 link here
    【解决方案3】:

    您可以使用DetectActivity 来区分预定义的活动类型。

    【讨论】:

      【解决方案4】:

      看看Google Location and Activity Recognition API。我认为这正是您想要的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-21
        • 2015-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-02
        相关资源
        最近更新 更多