0

i was made an geolocation application.. it was works successfully but always make a looping and showing toast continuously.. here's the code:

package com.application.geocoding;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

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

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

        geoLocation();

    }
    public void geoLocation()
    {
        LocationManager locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
              // Called when a new location is found by the network location provider.
              //makeUseOfNewLocation(location);
                updateWithNewLocation(location);
            }

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

            public void onProviderEnabled(String provider) {
                Toast.makeText(main.this,"Network Enabled",Toast.LENGTH_SHORT ).show();
            }

            public void onProviderDisabled(String provider) {
                Toast.makeText(main.this,"Network Disabled",Toast.LENGTH_SHORT ).show();
            }
          };

        // Register the listener with the Location Manager to receive location updates
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    }

    public void updateWithNewLocation(Location location) {
        String latLongString,addressString = "Location not found";
        TextView myLocationText,myLocationText2; 
        myLocationText=(TextView)findViewById(R.id.textView1);
        myLocationText2=(TextView)findViewById(R.id.textView2);

        if (location != null) {
          double lat = location.getLatitude();
          double lng = location.getLongitude();

          Geocoder gc = new Geocoder(this, Locale.getDefault());
          try {
            List<Address> addresses = gc.getFromLocation(lat, lng, 1);
            StringBuilder sb = new StringBuilder();
            if (addresses.size() > 0) {
              Address address = addresses.get(0);

              for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
                sb.append(address.getAddressLine(i)).append("\n");

                sb.append(address.getLocality()).append("\n");
                //sb.append(address.getPostalCode()).append("\n");
                sb.append(address.getCountryName());
            }
            addressString = sb.toString();
          } catch (IOException e) {}

          latLongString = "Lat:" + lat + "\nLong:" + lng;
        } else {
          latLongString = "No location found"; 
        }
        //myLocationText.setText("Your Current Position is:\n" + 
                               //latLongString);
        //myLocationText2.setText(addressString);
        Toast.makeText(this, latLongString, 1).show();
        Toast.makeText(this, addressString, 1).show();
    }

}

has anyone know where i should put this :

 locationManager.removeUpdates(locationListener);

so it will be running for once only?? thanks..

Michael Frans
  • 613
  • 3
  • 23
  • 49
  • [here](http://stackoverflow.com/questions/2021176/android-gps-status/3712727#3712727) you can find many answers... – Hanry Aug 06 '11 at 07:24

2 Answers2

2

You can put

locationManager.removeUpdates(locationListener); 
locationManager = null;

where ever you want to stop the Listener.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • i don't have button or something to trigger your code.. i want to run my GPS only once.. can you give me an example how to do that?? because i was implemented this code to other program.. thanks – Michael Frans Aug 06 '11 at 07:03
  • You can make some method in the Your class and access it from another class to stop it. – Lalit Poptani Aug 06 '11 at 07:20
0

You will want to put it into the Override onLocationChanged(Location)

@Override
public void onLocationChanged(Location location) {
    locationManager.removeUpdates(locationListener);
}

This will stop receiving location updates once you get the first GPS fix

hooked82
  • 6,336
  • 4
  • 41
  • 46