Android检测车辆是否移动

Android检测车辆是否移动,android,geolocation,accelerometer,activity-recognition,Android,Geolocation,Accelerometer,Activity Recognition,我正在开发一个类似于Uber的应用程序,我们需要计算车辆停止移动(或停止移动)时的等待时间 我尝试使用GPS定位速度,但似乎没有帮助。然后我们尝试了活动识别API,也没有立即提供更新 我们还有其他方法可以做到这一点吗?请注意,这不是检测速度,而是检测车辆是否移动 谢谢, 在我看来,您有3种选择: 您可以实现自己的驾驶检测技术,尽管我建议不要这样做,但不要重新发明方向盘,已经开发出了很好的API 你可以使用一个免费的sdk,当你的用户开始驾驶和结束驾驶时,它可以向你发送一个事件。 看看这个:基本上

我正在开发一个类似于Uber的应用程序,我们需要计算车辆停止移动(或停止移动)时的等待时间 我尝试使用GPS定位速度,但似乎没有帮助。然后我们尝试了活动识别API,也没有立即提供更新

我们还有其他方法可以做到这一点吗?请注意,这不是检测速度,而是检测车辆是否移动

谢谢,
在我看来,您有3种选择:

  • 您可以实现自己的驾驶检测技术,尽管我建议不要这样做,但不要重新发明方向盘,已经开发出了很好的API
  • 你可以使用一个免费的sdk,当你的用户开始驾驶和结束驾驶时,它可以向你发送一个事件。 看看这个:基本上,当你开始驾驶时,项目会以文本到语音的方式发布通知,这不是你想要的,但是当Neura向你发送开始/完成驾驶的事件时,你可以做任何你想要的事情。 把这个项目变成你自己的项目是很容易的。 我强烈建议使用此选项。
  • 你可以使用谷歌的来申报驾驶围栏

    虽然这种方法看起来不错,但我遇到了这样一个事实,即该api有时没有告诉我用户何时开始/结束驾驶,有时在我开始驾驶后很长一段时间,api才告诉我该事件

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

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

    
    

    c、 您的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;
                }
            }
        }
    }
    
    公共类MainActivity扩展活动{
    私人GoogleapClient MGoogleapClient;
    私人悬而未决的侵权行为;
    私人接收机;
    //触发围栏时将触发的意图操作。
    私有最终字符串FENCE\u RECEIVER\u ACTION=BuildConfig.APPLICATION\u ID+“FENCE\u RECEIVER\u ACTION”;
    @凌驾
    创建时受保护的void(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mGoogleApiClient=new-GoogleApiClient.Builder(this.addApi(Awareness.API.build());
    mGoogleApiClient.connect();
    //设置触发围栏时将要发射的悬挂式帐篷。
    mPendingIntent=PendingIntent.getBroadcast(this,0,新意图(FENCE\u RECEIVER\u ACTION),0);
    //当栅栏被触发时接收意图的广播接收器。
    mFenceReceiver=新的FenceReceiver();
    注册接收者(mFenceReceiver,新意向过滤器(围栏接收者行动));
    createFence(车辆中检测到的活动围栏,“InVehicleFence”);
    }
    @凌驾
    公共空间{
    试一试{
    unregisterReceiver(mFenceReceiver);//别忘了注销接收器
    }捕获(例外e){
    e、 printStackTrace();
    }
    super.ondestory();
    }
    私有void createFence(int detectedActivityFence,最终字符串fenceKey){
    AwarenessFence=DetectedActivityFence.during(DetectedActivityFence);
    //注册围栏以接收回调。
    Awareness.FenceApi.updateFences(
    mGoogleApiClient,新FenceUpdateRequest.Builder().addFence(fenceKey,fence,mpendingContent)
    .build()).setResultCallback(新的ResultCallback()){
    @凌驾
    public void onResult(@NonNull状态){
    if(status.issucess()){
    Log.i(getClass().getSimpleName(),“已成功注册”);
    }否则{
    Log.e(getClass().getSimpleName(),“无法注册:”+状态);
    }
    }
    });
    }
    //根据意图处理回调。
    公共类FenceReceiver扩展了BroadcastReceiver{
    @凌驾
    公共void onReceive(上下文、意图){
    FenceState=FenceState.extract(intent);
    开关(fenceState.getCurrentState()){
    case FenceState.TRUE:
    Log.i(fenceState.getFenceKey(),“Driving”);
    打破
    case FenceState.FALSE:
    Log.i(fenceState.getFenceKey(),“非驱动”);
    打破
    }
    }
    }
    }
    

  • 您可以使用Accelerometer您提到您尝试了“活动识别API”它没有按预期工作。你是说你使用Google Play services位置API,但它没有工作吗?你是否将其设置为定期更新位置?你设置的间隔参数是什么?你是否检查了每个活动的置信度?请添加更多信息,以便我们能够帮助你。Matan…是的,我已经完成了所有这些在
    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;
                }
            }
        }
    }