Toast doesn't disappear from screen

删除回忆录丶 提交于 2020-01-05 03:59:05

问题


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

}

}


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/12200925/toast-doesnt-disappear-from-screen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!