简体   繁体   中英

GPS icon won't disappear when activity destroyed?

Being an Android newbie experimenting with GPS stuff I managed to put together this code and it works just like I expect it to except for one thing, the GPS icon never goes away. How can get the GPS icon to disappear when the activity is destroyed? I have

locationManager.removeUpdates(GPSMapTest.this);     
locationManager = null;

in my onPause() but apparently that's not enough? Thanks.

The problem exists in the emulator and also on my HTC EVO with 2.2. On my EVO, the icon stays there when the activity is destroyed and only disappears when I uninstall the app.

    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) {


    }

}

No one? I tried everything with my limited knowledge! Help!

I believe it is because you are using MyLocationOverlay and have enableMyLocation() :

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

If you remove that line of code everything will work fine. In the OnPause() you want to call:

myLocOverlay.disableMyLocation();

and in onResume() call:

myLocOverlay.enableMyLocation();

Following works, provided you store the MyLocOverlay Object as class variable "myLocOverlay" in your Activity. Prefer using onStop() over onPause() to avoid unnecessarily stopping and restarting the localization. onStop() is called when " the activity is no longer visible to the user ".

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

I do not have enough reputation to write a comment, but: "magaios" answer is wrong as seen in this official example . super.onPause() should be called first.

You are calling super.onPause() before removing updates! Your Activity is never getting around to it. Switch it around:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM