当活动被破坏时,GPS图标不会消失?

时间:2010-09-19 04:25:13

标签: android icons gps

作为一名尝试使用GPS的Android新手,我设法将这些代码放在一起,它就像我期望的那样,除了一件事,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) {


    }

}

没有人?我用有限的知识尝试了一切!救命啊!

3 个答案:

答案 0 :(得分:4)

我认为这是因为您使用MyLocationOverlay并拥有enableMyLocation()

MyLocationOverlay myLocOverlay = new MyLocationOverlay(this, mapView);
    myLocOverlay.enableMyLocation();
    mapOverlays.add(myLocOverlay);

如果删除该行代码,一切都会正常工作。在要调用的OnPause()中:

myLocOverlay.disableMyLocation();

并在onResume()致电:

myLocOverlay.enableMyLocation();

答案 1 :(得分:1)

以下工作原理,前提是您将MyLocOverlay对象存储为Activity中的类变量“myLocOverlay”。 首选使用onStop()而不是onPause(),以避免不必要地停止和重新启动本地化。当“该活动不再对用户可见”时会调用onStop()

  @Override
    protected void onStop() {
    super.onStop();
    myLocOverlay.disableMyLocation();
    mapOverlays.remove(myLocOverlay);
    }

我没有足够的声誉来撰写评论,但是:“magaios”答案错误,如official example所示。  首先应该调用super.onPause()

答案 2 :(得分:0)

在删除更新之前,您正在调用super.onPause()!您的Activity永远无法解决问题。切换它:

locationManager.removeUpdates(this);
super.onPause();