0

I am following the guide on Android Developer to retrieve location and I am trying to get the speed from it. I have written this code by following the guide but the Log doesnt print and the text doesnt update.

        final Button button = (Button) findViewById(R.id.button3);
       final TextView speed = (TextView) findViewById(R.id.textView5);

// Acquire a reference to the system Location Manager
        final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        final String locationProvider = LocationManager.GPS_PROVIDER;
// Define a listener that responds to location updates
        final LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                // Called when a new location is found by the network location provider.
                float getS=(location.getSpeed());
                speed.setText("Speed: "+getS);
            }

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

                Log.e("",""+status+" "+provider);
            }

            public void onProviderEnabled(String provider) {

            }

            public void onProviderDisabled(String provider) {}
        };


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (running == 0) {
                    running = 1;
                    button.setText("Stop measuring");
                    locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);

                } else {
                    running = 0;
                    button.setText("Start measuring");
                    locationManager.removeUpdates(locationListener);
                }
            }
        });
John Mill
  • 5
  • 1
  • 1
  • 4

1 Answers1

0

No problem seems in code. You are getting location using GPS_PROVODER. There may be a problem getting location from GPS.You should first verify whether GPS is working properly on your device or not. You can do this by using Google Map application and check location on moving the device.

atifali
  • 193
  • 16