user2943342
user2943342

Reputation: 45

Android: GPS coordinates working on Emulator but not on the phone

I am an android beginner and I need your help! I am building a very simple running app and would like to have gather information about the speed. For this I am trying to create a very simple GPS class in order to use the getspeed() method.

I have created a little test app to ensure that my code is sound. In this app I am using getLatitude() and getLongitude() too but these will not be useful for my final code - I only need the speed really.

So far, I have managed to successfully test the class on the emulator but not on the phone. I can see the coordinates being populated on the screen when using the DDMS coordinates (getspeed() remained 0 but I know I cant test that on the emulator). When I try the code on my phone I have no response at all - despite waiting for the GPS to "warm up" (I also see that he GPS works when I move to GoogleMaps).

The whole things is driving me crazy, I am not sure what is happening with the onLocationChanged() method so appreciate any help you can give me. Thanks!

The code is as per below:

    import android.app.Activity; 
    import android.content.Context; 
    import android.location.Location; 
    import android.location.LocationListener; 
    import android.location.LocationManager; 
    import android.os.Bundle; 
    import android.widget.TextView;
    import android.widget.Toast; 

    public class MainActivity extends Activity { 
    TextView speedText1, speedText2, speedText3;
double speed = 0;
double latitude = 0;
double longitude = 0;
String speed1;
String latitude1;
String longitude1;
boolean gps_enabled;

Context context;

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


    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);


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

            Toast.makeText(getBaseContext(), "location not null" , Toast.LENGTH_SHORT).show();


            speedText1 = (TextView) findViewById(R.id.textView1);
            speedText2 = (TextView) findViewById(R.id.textView2);
            speedText3 = (TextView) findViewById(R.id.textView3);

            speed = location.getSpeed();
            speed1 = Double.toString(speed);
            speedText1.setText(speed1);

            latitude = location.getLatitude();
            latitude1 = Double.toString(latitude);
            speedText2.setText(latitude1);

            longitude = location.getLongitude();
            longitude1 = Double.toString(longitude);
            speedText3.setText(longitude1);

             Toast.makeText(getBaseContext(), latitude1 + longitude1 + speed , Toast.LENGTH_SHORT).show();



        }     

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

        } 

        public void onProviderEnabled(String provider) { 

        } 

        public void onProviderDisabled(String provider) {

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

Upvotes: 2

Views: 1963

Answers (1)

Pablo
Pablo

Reputation: 1091

You are defining the LocationListener inside the onCreate() method, so you will not get location updates once that method finishes. You should try something like this:

import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.widget.TextView;
import android.widget.Toast; 

public class MainActivity extends Activity implements LocationListener { 

TextView speedText1, speedText2, speedText3;
double speed = 0;
double latitude = 0;
double longitude = 0;
String speed1;
String latitude1;
String longitude1;
boolean gps_enabled;

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


    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 0.0f, this);
    gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);


    Toast.makeText(this, "location not null" , Toast.LENGTH_SHORT).show();


    speedText1 = (TextView) findViewById(R.id.textView1);
    speedText2 = (TextView) findViewById(R.id.textView2);
    speedText3 = (TextView) findViewById(R.id.textView3);

    }

    @Override
    public void onLocationChanged(Location location) {

        speed = location.getSpeed();
        speed1 = Double.toString(speed);
        speedText1.setText(speed1);

        latitude = location.getLatitude();
        latitude1 = Double.toString(latitude);
        speedText2.setText(latitude1);

        longitude = location.getLongitude();
        longitude1 = Double.toString(longitude);
        speedText3.setText(longitude1);

        Toast.makeText(this, latitude1 + longitude1 + speed, Toast.LENGTH_SHORT).show();            

    }

}

}

Upvotes: 2

Related Questions