Android检测车辆是否移动

时间:2016-08-12 09:53:59

标签: android geolocation accelerometer activity-recognition

我正在开发类似优步的应用程序,如果车辆停止移动(或移动),我们需要计算等待时间

我尝试使用GPS定位速度,但它似乎没有帮助。然后我们尝试了活动识别API,它也没有立即更新。

还有其他方法可以实现这个目标吗?请注意它不是关于检测速度。它是关于检测车辆是否在移动。

谢谢, Aneef

1 个答案:

答案 0 :(得分:10)

据我所知,你有3个选择:

  1. 您可以使用Activity recognitionreceiving location updates实施自己的驾驶技术 - 虽然我建议不要这样做,不要重新发明轮子,有良好的apis已经发展。
  2. 您可以使用Neura的免费sdk,可以在您的用户开始驾驶并完成驾驶时向您发送活动。 看看这个git project:基本上,当你开始驾驶时,项目会发布带有文字转语音的通知,这不是你想要的,但是你可以做任何你想要的事情,当Neura向你发送一个事件开始/完成驾驶。 很容易就可以把这个项目变成你自己的项目。 我强烈建议您使用此选项。
  3. 您可以使用谷歌的FenceApi来宣布一个驾驶围栏。

    虽然这种方法似乎很好,但我面对这样一个事实:这个api有时在用户开始/结束驾驶时没有告诉我,有时我开始驾驶后需要很长时间,api告诉我这个事件

    一个。包括对应用程序的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(), "Driving");
                        break;
                    case FenceState.FALSE:
                        Log.i(fenceState.getFenceKey(), "Not driving");
                        break;
                }
            }
        }
    }