Search code examples
androidtoast

Toast doesn't disappear from screen


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

}

}


Solution

  • 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.