1

I have a problem with my simple android aplication that use GPS cords.

My class MyLocationListener implements LocationListener, and there is a static method call:

    String strText ="My current location is: " + "Latitude = " +  location.getLatitude() + " Longitude= " + location.getLongitude();
    Toast.makeText(GpsModule.cont, strText, Toast.LENGTH_SHORT).show();

Problem is with displaying this string. It's showing up, and never ends. When I press back button for main menu and even when I close application it's showing up constantly. Any clue? How can I resolve this problem?

GpsModule Class:

public class GpsModule extends Activity {

public static Context cont;
//public static WebView position;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gps);
    cont = getApplicationContext();
    //position = new WebView(cont);

    //position = (WebView) findViewById(R.layout.gps);
    //position.getSettings().setJavaScriptEnabled(true);


    LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    LocationListener locListener = new MyLocationListener();
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}

MyLocationListener class:

@SuppressLint("ShowToast")

public class MyLocationListener implements LocationListener{

public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    location.getLatitude();
    location.getLongitude();

    String strText ="My current location is: " + "Latitude = " +  location.getLatitude() + " Longitude= " + location.getLongitude();
    Toast.makeText(GpsModule.cont, strText, Toast.LENGTH_SHORT).show();

}

public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
    Toast.makeText(GpsModule.cont, "GPS disabled", Toast.LENGTH_SHORT).show();
}

public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
    Toast.makeText(GpsModule.cont, "GPS enabled", Toast.LENGTH_SHORT).show();
}

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

}

}

Mariusz Chw
  • 364
  • 2
  • 19

4 Answers4

3

I think, you add your code in your location detection function and LocationListener is active. That means your Toast is called when GPS detect new location.

Ratna Halder
  • 1,954
  • 16
  • 17
3

Please read the docs of the method LocationManager.requestLocationUpdates().

Especially the parameter:

    minTime     minimum time interval between location updates, in milliseconds.

Try value 10'000 for every 10 sec. In production you should consider value more than mins.

user802421
  • 7,465
  • 5
  • 40
  • 63
  • Thanks for help. There was 2 problem: - one with parameters in requestLocationUpdates() - You resolved this, and thanks a lot - one with showing Toast even after clicking Back button – Mariusz Chw Aug 30 '12 at 17:14
  • 1
    Toast showing doesn't happen on UI thread. I don't think you can change that. You can look in to notification api. – user802421 Aug 30 '12 at 17:44
1

Looks like your location listener is active and repeatedly gets called. are you moving your device? LocationListener is called when the location changes.

Visa
  • 111
  • 4
  • Even when phone is not moving, location is changing because of number precision. For example: 50,00443558333333 – Mariusz Chw Aug 30 '12 at 16:36
1

Try this,

1 .

Toast.makeText(getBaseContext(), strText, Toast.LENGTH_SHORT).show();

2 .

@Override 
public void onPause() {
    super.onPause();
    locationManager.removeUpdates(locationListener);
}

3 .

@Override
    public void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                Length, long, locationListener);
    }

Length and long should be more than 0.

Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51
Ratna Halder
  • 1,954
  • 16
  • 17
  • Thanks for help. There was 2 problem: - one with parameters in requestLocationUpdates () - one with showing Toast even after clicking Back button - You resolved this, and thanks a lot – Mariusz Chw Aug 30 '12 at 17:13