如何阻止GPS /位置跟踪,当我的活动结束? [英] How do I stop GPS/location tracking when my Activity finishes?

查看:136
本文介绍了如何阻止GPS /位置跟踪,当我的活动结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Android的一个非常简单的应用程序,将显示一个谷歌地图浏览,并使用GPS跟踪位置(基本上是像现在这样):

I have a very simple app for Android that displays a Google Maps view and uses the GPS to track the position (essentially like so):

public void onCreate(Bundle savedInstanceState) {
    // ...
    mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    // ...
}

public void onResume() {
    super.onResume();
    mLocationManager.requestLocationUpdates(mProvider, 20000, 1, this);
}

public void onPause() {
    super.onPause();
    mLocationManager.removeUpdates(this);
}

public void onLocationChanged(Location location) {
    mPosition = getGeoPointForLocation(location);
    mMapController.setCenter(mPosition);
}

而当我用下面的命令退出应用程序(例如,通过菜单),的GPS不断跟踪 - 似乎活动仍在运行:

And when I use the following command to exit the application (e.g. through a menu), the GPS keeps on tracking - it seems that the Activity is still running:

// ...
case R.id.menu_exit:
    finish();
// ...

如何停止的GPS跟踪,如果它不被删除的位置经理的onPause()并调用完成()?至于我已经阅读教程或其他问题,这应该是解决方案。

How do I stop the GPS tracking if it does not work by removing the location manager in onPause() and calling finish()? As far as I have read tutorials or other questions, this should be the solution..

推荐答案

您使用MyLocationOverlay?这给你的脉动蓝点,表示重新presents您的当前位置。我没有看到MyLocationOverlay在样品code,但我只是双重检查。

Are you using MyLocationOverlay? This gives you the pulsating blue dot that represents your current position. I didn't see MyLocationOverlay in your sample code but I'm just double checking.

如果您使用的是MyLocationOverlay ...

If you are using MyLocationOverlay...

我测试过低于code:有和没有呼叫disableMyLocation当我退出pressing一个退出菜单命令后一个示例应用程序。当我打电话disableMyLocation,该GPS跟踪关闭。如果我不叫呢,GPS跟踪停留在后,我称之为完成。

I've tested the code below: with and without calling "disableMyLocation" when I exit a sample app after pressing an "exit" menu command. When I call "disableMyLocation", the GPS tracking turns off. When I don't call it, the GPS tracking stays on after I call "finish".

    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.quit:

        myLocOverlay.disableMyLocation(); //turn off location
        mlocManager.removeUpdates(this); // no impact on GPS tracking
        finish(); //destroy Android app
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}

这篇关于如何阻止GPS /位置跟踪,当我的活动结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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