Android的LocationListener的不停止使用GPS [英] android LocationListener not stopping GPS use

查看:473
本文介绍了Android的LocationListener的不停止使用GPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的服务,在某些情况下,我不能阻止搜索和排水设备的电池的GPS。

In my service, in some scenarios I can't stop the GPS from searching and draining the device battery.

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;
}

在我的应用程序一些地方,当我的主要活动,这破坏了应用preSS后退,这就是所谓的:

From some places in my app when I press "back" on the main activity, which destroys the app, this is called:

@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 生命周期方法需要是code的方法之前。

maybe my super.On lifecycle methods need to be before the code with the method.

还有什么我需要做什么来阻止GPS?我见过有这个问题,其他人的答案似乎并不那么绝对。

What else do I need to do to stop the GPS??? I've seen other people having problems with this and the answers don't seem so absolute.

在我的应用程序的其他情形中,GPS确实停止。

In other scenarios in my app, the GPS does stop.

推荐答案

我解决了我的问题。我知道,我并没有在原岗位提的MapView,但如果你同时使用图形页面的解决方案是图形页面可以使用GPS服务无关的自己的GPS定位监听服务。

I solved my issue. I know I didn't mention mapview in the original post but if you are also using MapView the solution is that mapview can use a GPS service unrelated to your own gps location listening service.

在您的图形页面

私人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();
}

这篇关于Android的LocationListener的不停止使用GPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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