Make sure you add the line
<permission android:name="android.permission.ACCESS_FINE_LOCATION"></
uses-permission>
to your AndroidManifest.xml file and you have enabled GPS satellites
on your handset (won't work otherwise).

Then try this:

public class GPSTest extends Activity {
        private TextView text;
    private LocationManager manager;
    private LocationListener listener;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        text = (TextView)findViewById(R.id.Text);

        manager = (LocationManager) getSystemService
(Context.LOCATION_SERVICE);
        Location myLocation = manager.getLastKnownLocation("gps");

        text.setText(("Lat: " + myLocation.getLatitude()
                        + "\nLLong: " + myLocation.getLongitude()));

        listener = new MyLocationListener();
        manager.requestLocationUpdates("gps" ,10000L, 10.0f,
listener);
    }

    private class MyLocationListener implements LocationListener{

        public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                if (location != null){
                        text.setText(("Lat: " + location.getLatitude()
                                + "\nLLong: " + location.getLongitude()));
                }
        }

        public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub
        }

        public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub
        }

        public void onStatusChanged(String provider, int status,
Bundle extras) {
                // TODO Auto-generated method stub
        }
    }
}

On Sep 4, 8:49 am, Xster <[email protected]> wrote:
> Hi,
>
> I'm trying to start a GPS program and I'm just trying out the first
> step to display coordinates as they change.
>
> I followedhttp://www.devx.com/wireless/Article/39239and used the
> LocationManager and LocationListener classes.
>
> Code:
> public class GPSTest extends Activity {
>     /** Called when the activity is first created. */
>
>         private TextView text;
>         private LocationManager manager;
>         private LocationListener listener;
>
>     @Override
>     public void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>         setContentView(R.layout.main);
>
>         text = (TextView) findViewById(R.id.Text);
>
>         manager = (LocationManager) getSystemService
> (Context.LOCATION_SERVICE);
>         listener = new MyLocationListener();
>         manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
> 0, 0, listener);
>     }
>
>     private class MyLocationListener implements LocationListener{
>
>                 public void onLocationChanged(Location location) {
>                         // TODO Auto-generated method stub
>                         if (location != null){
>                                 text.setText(text.getText() + "\n" + 
> location.getLatitude() + ", "
> + location.getLongitude() + " - " + location.getAccuracy());
>                         }
>                 }
>
>                 public void onProviderDisabled(String provider) {
>                         // TODO Auto-generated method stub
>
>                 }
>
>                 public void onProviderEnabled(String provider) {
>                         // TODO Auto-generated method stub
>
>                 }
>
>                 public void onStatusChanged(String provider, int status, 
> Bundle
> extras) {
>                         // TODO Auto-generated method stub
>
>                 }
>
>     }
>
> }
>
> However, once I run the code on the phone, the GPS icon is seen
> flashing but nothing happens. The method onLocationChanged is never
> reached (according to breakpoints). What am I doing wrong?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to