我使用GPS线的简单android应用存在问题。
我的类MyLocationListener实现了LocationListener,并且有一个静态方法调用:
String strText ="My current location is: " + "Latitude = " + location.getLatitude() + " Longitude= " + location.getLongitude();
Toast.makeText(GpsModule.cont, strText, Toast.LENGTH_SHORT).show();
问题在于显示此字符串。它正在显示,并且永远不会结束。当我按下主菜单的返回按钮时,即使我关闭应用程序,它也会不断显示。有什么线索吗?我该如何解决这个问题?
GpsModule类别:
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类:
@SuppressLint("ShowToast")
公共类MyLocationListener实现了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
}
}
最佳答案
请阅读方法LocationManager.requestLocationUpdates()的文档。
特别是参数:
minTime minimum time interval between location updates, in milliseconds.
每10秒尝试值10,000。在生产中,您应该考虑的价值不只是分钟。