作为一个Android新手试验GPS的东西,我设法把这些代码放在一起,它的工作方式就像我期望的那样,除了一件事情,GPS图标永远不会消失。当活动被破坏时,如何让GPS图标消失?我有当活动被破坏时,GPS图标不会消失?
locationManager.removeUpdates(GPSMapTest.this);
locationManager = null;
在我的onPause()
但显然这还不够?谢谢。
该问题存在于模拟器中,也存在于我的HTC EVO 2.2中。在我的EVO上,当活动被销毁时,图标会保留在那里,并且只有在我卸载应用程序时才会消失。
public class GPSMapTest extends MapActivity implements LocationListener, OnClickListener {
/** Called when the activity is first created. */
private MapView mapView;
private MapController mapController;
private LocationManager locationManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSPARENT);
setContentView(R.layout.main);
mapView = (MapView)findViewById(R.id.mapview);
mapController = mapView.getController();
mapController.setZoom(18);
mapView.setClickable(true);
mapView.setEnabled(true);
mapView.setSatellite(true);
Button buttonCurrentLoc = (Button)findViewById(R.id.myloc_btn);
buttonCurrentLoc.setOnClickListener(this);
}//End onCreate()
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(GPSMapTest.this);
locationManager = null;
}
@Override
protected void onResume() {
super.onResume();
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(false);
final String bestProvider = locationManager.getBestProvider(criteria, true);
Toast.makeText(GPSMapTest.this, "Best Provider: " + bestProvider, Toast.LENGTH_SHORT).show();
locationManager.requestLocationUpdates(bestProvider, 60000, 1, GPSMapTest.this);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
Double latitude = location.getLatitude()*1E6;
Double longitude = location.getLongitude()*1E6;
GeoPoint point = new GeoPoint(latitude.intValue(),longitude.intValue());
final MyLocationOverlay myLocationOverlay = new MyLocationOverlay(GPSMapTest.this, mapView);
mapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.enableMyLocation();
mapController.animateTo(point);
}
mapView.setBuiltInZoomControls(true);
}
public void onClick(View v) {
switch (v.getId()) {
case (R.id.myloc_btn):
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(false);
final String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
Double latitude = location.getLatitude()*1E6;
Double longitude = location.getLongitude()*1E6;
GeoPoint point = new GeoPoint(latitude.intValue(),longitude.intValue());
final MyLocationOverlay myLocationOverlay = new MyLocationOverlay(GPSMapTest.this, mapView);
mapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.enableMyLocation();
mapController.animateTo(point);
}
mapView.setBuiltInZoomControls(true);
break;
}
}
public void onLocationChanged(Location location) {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (location != null) {
final MyLocationOverlay myLocationOverlay = new MyLocationOverlay(GPSMapTest.this, mapView);
mapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.enableMyLocation();
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
mapController.animateTo(myLocationOverlay.getMyLocation());
}
});
}
mapView.setBuiltInZoomControls(true);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
没有人?我用有限的知识尝试了一切!帮帮我!
查看修改后的答案,您先调用super.onPause(),最后调用它。 – magaio 2010-09-20 13:26:50