Detect user activity (running, cycling, driving) using Android

后端 未结 4 634
生来不讨喜
生来不讨喜 2021-02-01 07:39

Using my Android device how can I detect if the user is walking, cycling or driving? I have checked the Google Fit app. It differentiates between running, cycling and driving. I

4条回答
  •  耶瑟儿~
    2021-02-01 08:19

    This question is quite old, but since there are new technologies out there, i thought it was worth mentioning, if anyone is still encountering this issue.

    I can come up with 3 options :

    1. You can implement your own technique for detecting walking, driving, cycling - using Activity recognition and receiving location updates, though i recommend not to do so, don't reinvent the wheel, there are good apis developed already, it's 2016 now.
    2. You could use a free sdk of Neura which can send you an event when your user starts/finishes driving, start/finish walking, start/finish running, read more of the events you can get from Neura.

      Check out this git project : Basically, the project has all the events that Nuera can detect. Its very easy to just take this project and make it your own.

      I highly recommend using this Neura sdk option.

    3. You could use google's FenceApi in order to declare fences. For example, this is a code for detecting a driving fence.

      Though this approach seems good, i've faced with the fact that this api didn't tell me sometimes when the events happened, and sometimes it took a long time after i started walking/running when the api told me of that event.

      a. include dependency to your app's build.gradle file :

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

      b. Manifest definitions :

      
      
      
      
      
      
          
      
          
              
                  
      
                  
              
          
      
      

      PUT_YOUR_AWARENESS_KEY_HERE : You need to generate a key here.

      c. Your MainActivity class - explanations attached to the code :

      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() {
                  @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;
                  }
              }
          }
      }
      

      This sample is only for detecting driving state, but, you can call 'createFence' with other activity methods such as :

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

提交回复
热议问题