当活动破坏GPS图标将不会消失? [英] GPS icon won't disappear when activity destroyed?

查看:207
本文介绍了当活动破坏GPS图标将不会消失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一款Android新手与GPS尝试的东西我设法把这个共同code和它的作品就像我期望它除了一件事,在GPS图标永远不会消失。怎样才能当活动被破坏的GPS图标消失?我有

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;

在我的的onPause()但显然这还不够?谢谢你。

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

在模拟器中,也对我的HTC EVO 2.2存在的问题。在我的EVO,该图标在那里停留,当活动被破坏,只有当我卸载该应用程序会消失。

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!

推荐答案

我相信这是因为你使用 MyLocationOverlay enableMyLocation()

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

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

如果您删除该行code一切将正常工作。在的onPause()你要调用:

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

myLocOverlay.disableMyLocation();

onResume()电话:

myLocOverlay.enableMyLocation();

这篇关于当活动破坏GPS图标将不会消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆