android - 使用 Android 检测用户 Activity (运行、骑自行车、驾驶)

标签 android accelerometer android-sensors gyroscope

如何使用我的 Android 设备检测用户是在步行、骑自行车还是在开车? 我检查了 Google Fit app.它区分运行、骑自行车和驾驶。我对应该使用什么算法来区分这些 Activity 感到困惑。

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

最佳答案

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

我可以想出 3 个选项:

  1. 您可以实现自己的技术来检测步行、驾驶、骑自行车 - 使用 Activity recognitionreceiving location updates ,虽然我建议不要这样做,但不要重新发明轮子,已经开发了很好的 api,现在是 2016 年了。
  2. 您可以使用免费的 sdk Neura它可以在您的用户开始/结束驾驶、开始/结束步行、开始/结束运行时向您发送事件,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:+'
    

    list 定义:

    <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 :您需要生成一个 key 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;
                }
            }
        }
    }
    

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

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

关于android - 使用 Android 检测用户 Activity (运行、骑自行车、驾驶),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27058741/

相关文章:

android - 运行 gradlew 测试时出现 Robolectric 错误

c# - Windows 通用应用程序实时加速度计数据图表

objective-c - 你如何使用移动平均线来过滤掉 iPhone OS 中的加速度计值

android - 这是检查系统上是否存在罗盘传感器的正确方法吗?

android - recyclerview下的按钮不可见

android - 使 AndroidTV 应用程序可在 FireTV 上运行

java - Android后台服务错误: Can't create handler inside thread that has not called

android - 在android中测量设备的精确旋转角度

android - 检测设备是否被拿着或放在 table 上

android - 如何使用 C/C++ 玩加速度计和相机等设备?