6

I have unsuccessfully attempted to get GPS Location to work for my app under Android. I would like to know what I might be doing wrong. I have so far tried the following methods:

as well as many others, but all in vain.

Considering that all the solutions above worked for some or the other person, I will use this one to debug:

package my.namespace;

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.util.Log;
import android.widget.Toast;

public class HomeActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    LocationListener ll = new mylocationlistener();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
    }

    private class mylocationlistener implements LocationListener {
    public void onLocationChanged(Location location) {
        if (location != null) {
        Log.d("LOCATION CHANGED", location.getLatitude() + "");
        Log.d("LOCATION CHANGED", location.getLongitude() + "");
        Toast.makeText(HomeActivity.this,
            location.getLatitude() + "" + location.getLongitude(),
            Toast.LENGTH_LONG).show();
        }
    }
    public void onProviderDisabled(String provider) {
    }
    public void onProviderEnabled(String provider) {
    }
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
    }
}

There were @override annotations for the 3 methods onProviderDisabled, onProviderEnabled and onStatusChanged. I had to remove them because Eclipse gave an error that these methods are not overriding a superclass.

I have added the permissions in the manifest file (ACCESS_FINE_LOCATION, ACCESS_MOCK_LOCATION, ACCESS_COARSE_LOCATION and INTERNET).

Does anyone have an idea what I might be doing wrong?

Edit: running on Emulator (Android 2.2 API 8)

Community
  • 1
  • 1
gkris
  • 1,209
  • 3
  • 17
  • 44

1 Answers1

3

Make sure to enable GPS when you test your app. It would also help if you use NETWORK_PROVIDER and GPS_PROVIDER both. So that when GPS is not available you still get the location with NETWORK_PROVIDER.

UPDATE: Also, if you are using emulator to test your app, make sure you emulate the GPS fix either from command line or using Eclipse. here is a link to stackoverflow question about how to do this How to emulate GPS location in the Android Emulator?

Community
  • 1
  • 1
binW
  • 13,220
  • 11
  • 56
  • 69
  • Thank you for the reply. I dont think i can add NETWORK_PROVIDER, because i am testing this on a simulator. And I am already passing GPS_PROVIDER as a parameter to requestLocationUpdates method. – gkris Apr 26 '12 at 11:27
  • means you are testing your gps based app in emmulator ? – MAC Apr 26 '12 at 11:31
  • Yes but you can use both providers if you want to. Anyway if you are testing it on emulator, are you passing then simulating location fixes? either from command line or from eclipse. Because on emulator the app will get a location update only when you simulate a location fix. – binW Apr 26 '12 at 11:31
  • Yes I am using the emulator to test this app. I did not know about the location simulation for emulators. Is it like passing some argument? I will search how to do it. Thanks a lot. – gkris Apr 26 '12 at 11:35
  • @gkris Glad to be of assistance. Remember to accept the answer if it helped. I have added a link to a question about emulating gps fix on emulator. You can use it to do this. – binW Apr 26 '12 at 11:39
  • thank you for your reply. I am not able to open the terminal but I am able to send the location using Emulator Control in eclipse. – gkris Apr 26 '12 at 11:47