1

How to get the speed of a vehicle using your phone when we seated in the vehicle using GPS in Android latest version. I have used the GPS_PROVIDER location.getSpeed() and its not working. I have used the below mentioned code which is not working and my phone Model is Samsung m21 (Android 12 OS).

In the below code onLocationChanged @Override function is not working.

  1. Can we track speed with GPS provider in latest (Android 12) phones?

If it is possible please let me know the new process of getting the speed of vehicle using GPS provider.

Appreciate the help, Thanks.

private class SpeedTask extends AsyncTask<String, Void, String> {

        final GpsMainActivity activity;
        float speed = 0.0f;
        double lat;
        LocationManager locationManager;

        public SpeedTask(GpsMainActivity activity) {
            this.activity = activity;
        }

        @Override
        protected String doInBackground(String... params) {
            locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
            return null;

        }

        protected void onPostExecute(String result) {
            tvUnit.setText("abc " + unit[unitType - 1]);
            LocationListener listener = new LocationListener() {
                float filtSpeed;
                float localspeed;

                @Override //not entered to this method
                public void onLocationChanged(Location location) {
                    speed = location.getSpeed();
                    System.out.println("SpeedHere:: "+speed); 
                    float multiplier = 3.6f;

                    switch (unitType) {
                        case 1:
                            multiplier = 3.6f;
                            break;
                        case 2:
                            multiplier = 2.25f;
                            break;
                        case 3:
                            multiplier = 1.0f;
                            break;

                        case 4:
                            multiplier = 1.943856f;
                            break;

                    }

                    if (maxSpeed < speed) {
                        maxSpeed = speed;
                    }


                    localspeed = speed * multiplier;
                    filtSpeed = filter(filtSpeed, localspeed, 2);
                    
                    NumberFormat numberFormat = NumberFormat.getNumberInstance();
                    numberFormat.setMaximumFractionDigits(0);


                    lat = location.getLatitude();
                    //speed=(float) location.getLatitude();
                    System.out.println("here_enter1");
                    Log.d("net.mypapit.speedview", "Speed " + localspeed + "latitude: " + lat + " longitude: " + location.getLongitude());
                    tvSpeed.setText("tvSpeed " + numberFormat.format(filtSpeed));

                    tvMaxSpeed.setText("hello " + numberFormat.format(maxSpeed * multiplier));

                    if (location.hasAltitude()) {
                        tvAccuracy.setText("stu " + numberFormat.format(location.getAccuracy()) + " m");
                    } else {
                        tvAccuracy.setText("hi NIL");
                    }

                    numberFormat.setMaximumFractionDigits(0);


                    if (location.hasBearing()) {

                        double bearing = location.getBearing();
                        String strBearing = "NIL";
                        if (bearing < 20.0) {
                            strBearing = "North";
                        } else if (bearing < 65.0) {
                            strBearing = "North-East";
                        } else if (bearing < 110.0) {
                            strBearing = "East";
                        } else if (bearing < 155.0) {
                            strBearing = "South-East";
                        } else if (bearing < 200.0) {
                            strBearing = "South";
                        } else if (bearing < 250.0) {
                            strBearing = "South-West";
                        } else if (bearing < 290.0) {
                            strBearing = "West";
                        } else if (bearing < 345.0) {
                            strBearing = "North-West";
                        } else if (bearing < 361.0) {
                            strBearing = "North";
                        }

                        tvHeading.setText(strBearing);
                    } else {
                        tvHeading.setText("NIL");
                    }

                    NumberFormat nf = NumberFormat.getInstance();

                    nf.setMaximumFractionDigits(4);


                    tvLat.setText(nf.format(location.getLatitude()));
                    tvLon.setText(nf.format(location.getLongitude()));


                }

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

                @Override
                public void onProviderEnabled(String provider) {
                

                }

                @Override
                public void onProviderDisabled(String provider) {
                  


                }

            };


            if (ActivityCompat.checkSelfPermission(GpsMainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(GpsMainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);

        }
        
        private float filter(final float prev, final float curr, final int ratio) {
            // If first time through, initialise digital filter with current values
            if (Float.isNaN(prev))
                return curr;
            // If current value is invalid, return previous filtered value
            if (Float.isNaN(curr))
                return prev;
            // Calculate new filtered value
            return (float) (curr / ratio + prev * (1.0 - 1.0 / ratio));
        }
    }
Jass Tech
  • 31
  • 4
  • Define not working. Does it crash? Return 0? Return some other value? – Gabe Sechan Sep 08 '22 at 04:49
  • Also- why is there an AsyncTask in here at all? Not only is AsyncTask deprecated, in this case it isn't doing anything. The only thing you're doing on the other thread is calling getSystemService, which can be done just fine on the main thread. – Gabe Sechan Sep 08 '22 at 04:51

0 Answers0