-2

I am planning to use a GPS enabled Android phone to create an application that will not use the internet to access the user's current position.

What I need to know is how to code it in Eclipse. Can you share a piece of code in Java that will return the longitude and latitude of the GPS user without accessing the internet?

Also, is it possible to make it so that it will not access the cell network- getting the user's position directly from the satellite?

snowflakekiller
  • 3,428
  • 6
  • 27
  • 45
rahstame
  • 469
  • 1
  • 7
  • 21

1 Answers1

3

Add the following line to your Android Manifest, above the application tag:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Then you'll need an Activity or a Service that has code like this:

LocationManager locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
    onLocationChanged(Location location) {
         // put code in here to respond to a change in location
    }
    onProviderDisabled(String provider) {
         // put code in here to respond to GPS being disabled
    }
    onProviderEnabled(String provider) {
        // put code in here to respond to GPS being enabled
    }
    onStatusChanged(String provider, int status, Bundle extras) {
        // put code in here to respond to change in GPS status (e.g. GPS becomes unavailable)
    }
});

Edit: When you call LocationManager locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener()... this sets a new LocationListener that listens to the GPS. The first number indicates the minimum number of milliseconds between each check (note this is just an indicator, the system may check more/less frequently), and the second number indicates the minimum distance travelled in metres between each check. So if both are 0, then the GPS will be checked constantly, and new values will be sent to onLocationChanged() as soon as they are available.

For more detailed information, see http://developer.android.com/guide/topics/location/obtaining-user-location.html

Pikaling
  • 8,282
  • 3
  • 27
  • 44