如何保持后台gps服务的活力[英] How to keep background gps service alive

本文是小编为大家收集整理的关于如何保持后台gps服务的活力的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我试图将背景GPS位置侦听器作为服务,该服务可以由我的应用程序中的所有活动使用.它也应该扫描位置,直到我"杀死"它.但是我意识到,几个小时后,全科医生服务被杀死,我再也无法获得位置.

我如何保持此服务的活力(至少是位置管理器和位置侦听器),直到我想要它?

?

谢谢

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

}

推荐答案

但是,我意识到,几个小时后,GPS服务被杀死了,我再也无法获得位置.

首先,在所有时间内保持GPS供电是一个异常坏的主意,因为用户的电池寿命将极大地遭受损失.您的申请需要提供巨大的价值(例如,Google导航)来保证此电源成本.

第二,切勿从IntentService中注册听众.一旦onHandleIntent()结束,服务就会关闭...但是您泄漏了注册的侦听器.这有效地保持了背景线程.但是,由于您没有活动组件,因此Android最终将终止您的流程.

本文地址:https://www.itbaoku.cn/post/102668.html

问题描述

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.