【问题标题】:android LocationListener not stopping GPS useandroid LocationListener 没有停止 GPS 的使用
【发布时间】:2012-11-21 15:19:33
【问题描述】:

在我的service 中,在某些情况下,我无法阻止 GPS 搜索和耗尽设备电池。

LocationListenerAgent.java

@Override
public void onCreate() {
    thisContext = this;
    // Register ourselves for location updates

    networkListener = new LocationListenerAgent();
    gpsListener = new LocationListenerAgent();

    lmgrNet = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
    lmgrGps = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
    gps_enabled = lmgrGps.isProviderEnabled(LocationManager.GPS_PROVIDER);
    lmgrNet.requestLocationUpdates("network", ONE_MINUTE, 10, networkListener);
    lmgrGps.requestLocationUpdates("gps", ONE_MINUTE, 10, gpsListener);

    super.onCreate();
}

@Override
public void onDestroy() {
    super.onDestroy();
    // Unregister listener
    lmgrNet.removeUpdates(this);
    lmgrNet = null;
    lmgrGps.removeUpdates(this);
    lmgrGps = null;
}

当我在主 Activity 上按“返回”时,从我的应用程序的某些地方,这会破坏应用程序,这被称为:

@Override
protected void onStop() {
    if (mBound) {
        unbindService(mConnection); //this is a callback to the locationlisteneragent class to bind it to a service
        mBound = false;
    }
    super.onStop();
}
@Override
protected void onDestroy() {

    if (mBound) {
        unbindService(mConnection);
        mBound = false;
    }
    stopService(new Intent(this, LocationListenerAgent.class));
    super.onDestroy();
}

也许我的super.On 生命周期方法需要在该方法的代码之前。

我还需要做什么才能停止 GPS ???我见过其他人对此有疑问,答案似乎并不那么绝对。

在我的应用中的其他情况下,GPS 确实会停止。

【问题讨论】:

    标签: android service gps locationlistener location-provider


    【解决方案1】:

    我解决了我的问题。我知道我在原始帖子中没有提到 mapview,但如果您也在使用 MapView,解决方案是 mapview 可以使用与您自己的 GPS 位置侦听服务无关的 GPS 服务。

    在您的地图视图中

    private MyLocationOverlay currLocationOverlay;

    @Override
    protected void onStop() {
        if(currLocationOverlay!=null)
            currLocationOverlay.disableMyLocation();
        super.onStop();
    }
    @Override
    protected void onDestroy() {
        if(currLocationOverlay!=null)
            currLocationOverlay.disableMyLocation();
        stopService(new Intent(this, LocationListenerAgent.class));
        super.onDestroy();
    }
    

    【讨论】:

      【解决方案2】:

      根据您发布的代码,我认为您没有删除位置更新。

      在你的 onDestroy() 方法中应该是:

      lmgrNet.removeUpdates(networkListener);
      lmgrNet.removeUpdates(gpsListener);
      

      【讨论】:

      • 我正在删除位置更新,再看一遍。不过我解决了,谢谢。
      • 我的错。如果 LocationListenerAgent 是您的服务,为什么要为侦听器创建 2 个附加对象而不是使用 this?此外,我认为您不需要为不同的提供者使用 2 个不同的 LocationManager。
      • 我尝试了两种方式。我实际上将听众分开,因为该站点上的另一个答案表明“removeUpdates”不会删除所有听众,并且该问题的解决方案是将提供者放在不同的位置管理器中,我希望这是有道理的,但你可以看到我做了什么在我的代码中
      【解决方案3】:

      你也可以这样停下来

      stopService(new Intent(AlertsActivity.this,PollingService.class)); android.os.Process.killProcess(android.os.Process.myPid());

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多