0

I am fairly new to Android development, and I'm running into a strange issue with an app I'm trying to develop to get up-to-speed. Sometimes this app seems to work fine, but most of the times it does not. I'm trying to simply set the GPS lat/long of the device from an app; nothing more than that really. I've utilized a lot of knowledge gathered from other posts, but in the end, I'm not sure if my app doesn't work due to emulator quirks, or if it's my code. For reference, I'm using IntelliJ, I'm targeting 2.3.3, I have indeed enabled GPS on my emulator, and I am indeed setting the appropriate permissions in my manifest.

The odd thing is that it sometimes works fine when I open the emulator, then I telnet via terminal and set the geolocation via "geo fix", and then go back into the app, and it shows my GPS lat/long that I entered. But usually it just doesn't work at all. And I get the same sort of results when I attempt to do it programatically via mock-locations in the code.

Here is the code I have at the top of my onCreate() method (locationManager is a class-level field):

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationManager.addTestProvider(LocationManager.GPS_PROVIDER, false, false, false, false, false, true, true, 0, 5);

..And when the user types in a lat/long into the UI and taps a submit-button, the following code is called after the fields are extracted and some light validation is done:

Location mLocation = new Location(LocationManager.GPS_PROVIDER);
mLocation.setLatitude(Float.valueOf(inputLat));
mLocation.setLongitude(Float.valueOf(inputLong));
mLocation.setTime(System.currentTimeMillis());

locationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);
locationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, mLocation);

refreshTheLocation();

And for reference, the refreshTheLocation() method is:

public void refreshTheLocation() {
    LocationResult locationResult = new LocationResult(){
        @Override
        public void gotLocation(Location location){
            final Location finalLoc = location;

            if (location != null) {
                runOnUiThread(new Runnable() {
                    public void run() {
                        TextView gpsTextView = (TextView) findViewById(R.id.gps_label);
                        gpsTextView.setText("GPS DATA FOUND:\nLatitude: " + finalLoc.getLatitude() + "\nLongitude: " + finalLoc.getLongitude());
                     }
                });
            } else {
                runOnUiThread(new Runnable() {
                   public void run() {
                      TextView gpsTextView = (TextView) findViewById(R.id.gps_label);
                      gpsTextView.setText("GPS data not found");
                   }
                });
            }
        }
   };
   MyLocation myLocation = new MyLocation();
   myLocation.getLocation(this, locationResult);
}  

The "MyLocation" class is simply a copy/paste from the top-rated answer on this post:

What is the simplest and most robust way to get the user's current location on Android?

Any thoughts on what my problem could be? It seems about 1/5 of the time, it gets the GPS coordinates and shows them, but the other 4/5 of the time, it does not. And when it DOES work, it never updates the coordinates when I change them via the input fields and button.

Go easy on me, please, as I'm fairly new to this stuff. ;) Thanks in advance for any help you guys can offer!

Community
  • 1
  • 1
svguerin3
  • 2,433
  • 3
  • 29
  • 53

1 Answers1

1

If you are targeting 2.3.3 version and have been testing on the 2.3.3 version AVD, I suggest that you switch to the 2.3.3 Google APIs version of the API and then try out the mock locations.

Romin
  • 8,708
  • 2
  • 24
  • 28
  • Worked like a charm! That was it, thank you!! For reference, I found a post that said 2.3.3 had a bug with its GPS, so I was actually using 2.2 (I was wrong in my original post). But regardless, I need to be using 2.3.3, so I'm very glad you let me know about this. Out of curiosity, what is the difference between the Android and the Google version of those APIs? Should I be using Google ones at all times? Or what is the standard? – svguerin3 Jul 26 '12 at 14:20
  • If you are doing Google Maps integration (MapActivity, etc) in your application, then you need to use that version of the AVD. – Romin Jul 26 '12 at 14:57
  • Thanks Romin - That definitely makes sense, but I'm actually not using any kind of Google Maps integration in my app. The code I posted is actually the only code in the app. I'm guessing that the GPS stuff (or perhaps Mock Locations) requires it as well? – svguerin3 Jul 26 '12 at 15:10
  • Yes - it does need that but only because of the GPS Mock locations bug. Otherwise you do not need that version if you are not using any of the Map stuff. – Romin Jul 26 '12 at 15:51