如何保持背景的GPS服务活 [英] How to keep background gps service alive

查看:129
本文介绍了如何保持背景的GPS服务活的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个背景GPS定位监听器,可在我的应用程序所使用的所有活动的服务。这也应该是扫描的位置,直到我杀了。不过,我意识到,经过几个小时的GPS服务就会被杀死,我无法再得到位置。

我如何保持这种服务还活着(的locationManager和位置监听器至少),直到我想它了吗?

感谢

 公共类GPS扩展IntentService {
    公共静态LocationListener的loc_listener = NULL;
    公共静态LocationManager locationManager = NULL;

    公共GPS(){
        超级(全球定位系统);
    }

    @覆盖
    保护无效onHandleIntent(意向意图){
        locationManager =(LocationManager)getSystemService(LOCATION_SERVICE);
        如果(loc_listener == NULL){
            loc_listener =新LocationListener的(){

                @覆盖
                公共无效onStatusChanged(字符串商,INT地位,
                        捆绑演员){
                    // TODO自动生成方法存根

                }

                @覆盖
                公共无效onProviderEnabled(字符串提供商){
                    // TODO自动生成方法存根

                }

                @覆盖
                公共无效onProviderDisabled(字符串提供商){
                    // TODO自动生成方法存根

                }

                @覆盖
                公共无效onLocationChanged(位置定位){
                    // TODO自动生成方法存根

                }
            };
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,
                0,loc_listener);
    }

    公共静态无效killGPS(){
        如果(locationManager = NULL和放大器;!&安培;!loc_listener = NULL){
            locationManager.removeUpdates(loc_listener);
        }
    }

}
 

解决方案
  

不过,我意识到,经过几个小时的GPS服务就会被杀死,我无法再得到位置。

首先,这是一个非常糟糕的主意,让GPS通电所有的时间,为用户的电池寿命会大打折扣的。您的应用程序需要提供巨大的价值(例如,谷歌导航),以保证这款电源的成本。

二,从来没有从 IntentService 注册一个侦听器。一旦 onHandleIntent()结束,该服务将关闭......但是你会泄露你的注册的侦听器。这有效地保持一个后台线程去。但是,由于你没有活性成分,机器人最终将终止进程。

I'm trying to have a background gps location listener as a service that can be used by all activities in my app. It should also be scanning for locations until I "kill" it. However I realized that after a couple of hours the gps service gets killed and I can't get anymore locations.

How do I keep this service alive (the locationManager and location listener at least) until I want it off?

Thanks

public class GPS extends IntentService {
    public static LocationListener loc_listener = null;
    public static LocationManager locationManager = null;

    public GPS() {
        super("GPS");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (loc_listener == null) {
            loc_listener = new LocationListener() {

                @Override
                public void onStatusChanged(String provider, int status,
                        Bundle extras) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProviderEnabled(String provider) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProviderDisabled(String provider) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onLocationChanged(Location location) {
                    // TODO Auto-generated method stub

                }
            };
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, loc_listener);
    }

    public static void killGPS() {
        if (locationManager != null && loc_listener != null) {
            locationManager.removeUpdates(loc_listener);
        }
    }

}

解决方案

However I realized that after a couple of hours the gps service gets killed and I can't get anymore locations.

First, it is an exceptionally bad idea to keep GPS powered on all of the time, as the user's battery life will suffer greatly. Your application needs to offer tremendous value (e.g., Google Navigation) to warrant this power cost.

Second, never register a listener from an IntentService. Once onHandleIntent() ends, the service shuts down... but you leak your registered listener. This effectively keeps a background thread going. However, since you have no active components, Android eventually will terminate your process.

这篇关于如何保持背景的GPS服务活的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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