使用Android检测用户活动(跑步,骑车,驾驶)

时间:2014-11-21 10:05:56

标签: android accelerometer android-sensors gyroscope

使用我的Android设备如何检测用户是走路,骑自行车还是开车? 我检查过Google Fit app.它区分跑步,骑车和驾驶。我很困惑我应该用什么算法来区分这些活动。

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

4 个答案:

答案 0 :(得分:16)

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

我可以提出3个选项:

  1. 你可以使用Activity recognitionreceiving location updates来实现自己的行走,驾驶,骑行技术,但我建议不要这样做,不要重新发明轮子,已经有了很好的发展,现在是2016年。
  2. 您可以使用Neura的免费sdk,可以在您的用户开始/完成驾驶,开始/结束步行,开始/结束运行read more of the events you can get from Neura时向您发送一个事件。

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

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

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

    虽然这种做法看起来不错,但是我已经面对这样一个事实:这个api在事件发生的时候并没有告诉我,有时候我开始走路/跑步时需要很长时间告诉我那件事。

    一个。包括依赖于你的app的build.gradle文件:

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

    湾清单定义:

    <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

    ℃。您的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;
                }
            }
        }
    }
    

    此示例仅用于检测驾驶状态,但是,您可以拨打&#39; createFence&#39;与其他活动方法,如:

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

答案 1 :(得分:13)

您可以使用GooglePlayServices进行此操作。

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

https://developer.android.com/reference/com/google/android/gms/location/ActivityRecognitionClient.html

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

答案 2 :(得分:2)

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

答案 3 :(得分:0)

看看Google Location and Activity Recognition API。我认为这正是你所寻找的。