使用Android检测用户活动(跑步、骑自行车、开车)

使用Android检测用户活动(跑步、骑自行车、开车),android,accelerometer,android-sensors,gyroscope,Android,Accelerometer,Android Sensors,Gyroscope,使用我的Android设备,我如何检测用户是在走路、骑自行车还是开车? 我已经检查了跑步、骑自行车和开车之间的区别。我对应该使用什么算法来区分这些活动感到困惑 我知道我必须使用加速计传感器。但我仍然无法区分这些活动。您可以使用来区分预定义类型的活动。您可以使用GooglePlayServices进行区分 它为ActivityRecognition提供了特殊的API,该API返回用户活动,每个活动都具有置信度 这个问题已经很老了,但由于有新技术出现,如果还有人遇到这个问题,我认为值得一提 我可以

使用我的Android设备,我如何检测用户是在走路、骑自行车还是开车? 我已经检查了跑步、骑自行车和开车之间的区别。我对应该使用什么算法来区分这些活动感到困惑


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

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

您可以使用GooglePlayServices进行区分

它为ActivityRecognition提供了特殊的API,该API返回用户活动,每个活动都具有置信度


这个问题已经很老了,但由于有新技术出现,如果还有人遇到这个问题,我认为值得一提

我可以想出3个选项:

  • 您可以实施自己的技术来检测步行、驾驶、骑自行车,尽管我建议不要这样做,但不要重新发明轮子,已经开发出了很好的API,现在是2016年了
  • 你可以使用一个免费的sdk,当你的用户开始/结束驾驶、开始/结束步行、开始/结束跑步时,它可以向你发送一个事件

    看看这个:基本上,这个项目拥有Nuera可以检测到的所有事件。把这个项目变成你自己的项目是很容易的

    我强烈建议使用此Neura sdk选项。

  • 你可以使用谷歌的来声明围栏。 例如,这是用于检测行驶围栏的代码

    虽然这种方法看起来不错,但我遇到了这样一个事实,即该api有时没有告诉我事件发生的时间,有时在api告诉我该事件时,我在开始行走/跑步后花了很长时间

    a。包括应用程序的build.gradle文件的依赖项:

       compile 'com.google.android.gms:play-services-location:+'
    
       compile 'com.google.android.gms:play-services-contextmanager:+'
    
    b。清单定义:


  • 请看下面的图片。我想这正是你想要的。

    听起来不错。是否需要任何最低API级别,或者只需要play services库就可以了?仅供参考,Google有一个示例项目来说明如何使用此API。我想知道你为什么建议使用Neura而不是“ActivityRecognition”API。Neura SDK是否使用一些不同的API或方法来检测活动?更好吗?
    <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>
    
    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(DetectedActivityFence.TILTING, "TiltingFence");
    createFence(DetectedActivityFence.WALKING, "WalkingFence");
    createFence(DetectedActivityFence.ON_FOOT, "OnFootFence");
    createFence(DetectedActivityFence.RUNNING, "RunningFence");