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

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

问题描述

我正在尝试将后台 gps 位置侦听器作为一项服务,供我的应用程序中的所有活动使用.它还应该扫描位置,直到我杀死"它.但是我意识到,几个小时后,gps 服务被终止了,我再也找不到位置了.

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.

在我想要它关闭之前,我如何保持该服务(至少是 locationManager 和位置侦听器)处于活动状态?

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

谢谢

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 服务被终止,我再也找不到位置了.

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

首先,让 GPS 始终处于开启状态是一个非常糟糕的主意,因为用户的电池寿命会受到很大影响.您的应用程序需要提供巨大的价值(例如 Google 导航)来保证这种电力成本.

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.

其次,永远不要从 IntentService 注册监听器.一旦 onHandleIntent() 结束,服务就会关闭……但你会泄露你注册的监听器.这有效地保持了后台线程的运行.但是,由于您没有活动组件,Android 最终会终止您的进程.

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天全站免登陆