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

发布于 2024-09-24 11:51:56 字数 5467 浏览 3 评论 0原文

作为一名尝试 GPS 东西的 Android 新手,我设法将这段代码组合在一起,它的工作原理就像我期望的那样,除了一件事,GPS 图标永远不会消失。如何让GPS图标在Activity被破坏时消失? 我的 onPause() 中有

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

,但显然这还不够?谢谢。

该问题存在于模拟器中,也存在于我的 HTC EVO 2.2 上。在我的 EVO 上,当 Activity 被销毁时,该图标会保留在那里,只有在我卸载应用程序时才会消失。

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


    }

}

没有人?我以我有限的知识尝试了一切!帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

败给现实 2024-10-01 11:51:56

我相信这是因为您正在使用 MyLocationOverlay 并具有 enableMyLocation()

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

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

myLocOverlay.disableMyLocation();

onResume() 中调用:

myLocOverlay.enableMyLocation();

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();
为你鎻心 2024-10-01 11:51:56

如果您将 MyLocOverlay 对象存储为 Activity 中的类变量“myLocOverlay”,则可以进行以下工作。
优先使用 onStop() 而不是 onPause() 以避免不必要的停止和重新启动本地化。当“该 Activity 对用户不再可见”时,将调用 onStop()

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

我没有足够的声誉来写评论,但是:“magaios”答案是错误,如官方示例
应首先调用 super.onPause()

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.

唱一曲作罢 2024-10-01 11:51:56

您在删除更新之前调用 super.onPause()!您的 Activity 永远无法抽出时间来处理它。切换一下:

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

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

locationManager.removeUpdates(this);
super.onPause();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文