2010-09-19 110 views
2

作爲一個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) { 


    } 

} 

沒有人?我用有限的知識嘗試了一切!幫幫我!

+0

查看修改後的答案,您先調用super.onPause(),最後調用它。 – magaio 2010-09-20 13:26:50

回答

4

我相信這是因爲你使用MyLocationOverlay並有enableMyLocation()

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

如果你刪除了這一行代碼一切將正常工作。在的onPause()你想撥打:

myLocOverlay.disableMyLocation(); 

onResume()電話:

myLocOverlay.enableMyLocation(); 
+0

我注意到,我不小心在onCreate方法之外調用的方法中爲myLocationOverlay添加了一個新的構造函數。這導致一個新的LocationOverlay被實例化,而不會破壞舊的,並且保持GPS在後臺運行。 – Chrispix 2011-11-21 04:16:07

0

在刪除更新之前,您正在致電super.onPause()!你的Activity永遠不會接近它。開關使用它:

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

謝謝,但我試過這個...同樣的問題,GPS圖標仍然存在。 – ShadowGod 2010-09-20 21:37:03

1

以下的工作,只要你保存MyLocOverlay對象爲您的活動類變量 「myLocOverlay」。 優先使用onStop()而不是onPause()以避免不必要的停止和重新啓動本地化。當「該活動不再可見於用戶」時,調用onStop()

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

我沒有足夠的信譽來寫評論,但:「magaios」答案是錯誤的看到這個official example。應首先調用 super.onPause()

相關問題