即使位置正在更改,执行onLocationChanged()失败的原因是什么

时间:2019-04-18 17:10:48

标签: android

我试图基于Java类中基于location.speed()的车速实现事故检测算法,因此我已经将该算法编写为onLocationChanged()方法,但是无论如何都没有被调用。

android studio的版本是3.2.1,代码编写在SensorService.java类中,并且其对象是在MainActivity中创建的。

布尔布尔值isAccident(){

    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    // Define a listener that responds to location updates
    try {
        if (MainActivity.LOCATION_SERVICE.equals("ok")) {


            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 100, new LocationListener() {
                        @Override
                        public void onLocationChanged(Location location) {
                            Toast.makeText(context, "inside Loc changed", Toast.LENGTH_SHORT).show();
                            float[] SP = new float[30];
                            float sum = 0;
                            int ot1 = 0, ot2 = 0;
                            float mean, sdd = 0;
                            Toast.makeText(context, "Current speed:" + String.valueOf(location.getSpeed()),
                                    Toast.LENGTH_SHORT).show();
                            speed = location.getSpeed();
                            if (speed >= 24) {
                                if (ACC / 4 >= 1) {
                                    flag = true;
                                }
                            } else {
                                Calendar calendar = Calendar.getInstance();
                                ot1 = calendar.get(Calendar.SECOND);
                                ot2 = ot1;
                                int i = 0;
                                while ((ot1 - calendar.get(Calendar.SECOND)) != 30) {
                                    if (location.getSpeed() < 24) {
                                        if ((ot1 - calendar.get(Calendar.SECOND)) == 15) {
                                            ot2 = ot1;
                                        }
                                        sum = sum + location.getSpeed();
                                        SP[i] = location.getSpeed();
                                    } else {
                                        if ((ACC / 4) >= 1)
                                            flag = true;
                                    }
                                }
                                mean = sum / 30;
                                for (i = 0; i <= 29; i++) {
                                    sdd += Math.pow(SP[i] - mean, 2);
                                }
                                sdd = sdd / 30;
                                if ((ACC / 4 + sdd / 2.06) >= 2) {
                                    flag = true;
                                }
                            }

                            ot1 = ot2;
                        }


                        @Override
                        public void onStatusChanged(String provider, int status, Bundle extras) {

                        }

                        @Override
                        public void onProviderEnabled(String provider) {

                        }

                        @Override
                        public void onProviderDisabled(String provider) {

                        }
                    }
            );
        }
    }
catch(SecurityException e)
{
    Log.d("error",""+e);
}




    return flag;
}

我希望调用应该转到onLoactionChanged(),但它会捕获()。

0 个答案:

没有答案