2

i am trying to develop a speedometer app for testing purposes i have already written the code that will get the speed but i am not sure if it is the real time speed because sometimes i see that there's some delay, this is my code:

public class MainActivity extends AppCompatActivity {

LocationManager locationManager;
LocationListener locationListener;

 public void startListening() {

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        if (locationManager != null) {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        }
    }

}


public void updateLocationInfo(Location location){

    TextView speedtextView = (TextView) findViewById(R.id.speedTextView);

    speedtextView.setText("Current speed: " + location.getSpeed()*3600/1000);

 }


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == 1) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            startListening();

        }

    }}




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {

            updateLocationInfo(location);

        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }
    };


    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);

    } else {

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if (location != null) {

            updateLocationInfo(location);

        }

    }

}
}

do i have to do anything else? is there any special way to get the real time speed? because i have used a lot of apps that gets the real time speed and right now i am wondering how did they do it? could it possibly be the "fair" GPS connection? or am i doing it wrong?

Sapien_Knight
  • 244
  • 3
  • 14
  • 1
    duplicate, https://stackoverflow.com/questions/15570542/determining-the-speed-of-a-vehicle-using-gps-in-android – Matt Wolfe Jun 15 '18 at 22:22
  • hello, i have seen this post already and unfortunately it dos not have the answer, the poster is using the same method i am using winch is location.getSpeed my question is am i doing it right or there is another way to track the LIVE speed – Sapien_Knight Jun 15 '18 at 22:24
  • But this is the same question as the other. If there is no answer then star the other question/upvote it but no point in reposting the same information in another thread. – Matt Wolfe Jun 15 '18 at 22:25
  • 1
    i am sorry, but i did not read anything related to LIVE speed in his article forgive me, i have the right to ask my question specifically! – Sapien_Knight Jun 15 '18 at 22:27
  • Well if it were me I'd implement this by using google play location services with the fused provider and simply calculate the distance when the location is updated.. Supposedly that provider is more accurate.. I'm not sure if it provides you with good speed information in the location data itself or if you need to compute it. – Matt Wolfe Jun 15 '18 at 22:32
  • https://developers.google.com/location-context/fused-location-provider/ – Matt Wolfe Jun 15 '18 at 22:33
  • i have already tried this, but it provides even less accurate speed i intend to transform this app into another idea, and the idea really requires very accurate speed data collection, anyway thank you! – Sapien_Knight Jun 15 '18 at 22:34
  • Did you calculate the speed or just use the getSpeed method from the location? Also, you will want to ensure it is using the GPS provider .. Lastly.. Try building an app that runs location in the background as a service.. Everytime you get an update, print it out as a toast message.. Then, open up waze or other realtime speed app and compare... – Matt Wolfe Jun 15 '18 at 22:39
  • now that's a nice method to test thank you, i currently don' have access to my workstations so i will just collect the answers and try the good ones like yours tomorrow, thanks for your help. – Sapien_Knight Jun 15 '18 at 22:42

0 Answers0