Android 服务中的 GPS 进入睡眠模式

发布于 2025-01-08 15:43:52 字数 2152 浏览 1 评论 0原文

我创建了一个 GPS 服务,它定期读取位置更新。早些时候存在一个问题,GPS 一段时间后进入睡眠模式,此后没有提供位置更新。

现在,我已更改代码以定期取消注册和注册 GPS 位置更新。这解决了我的 GPS 进入睡眠模式的问题,但又产生了一个新问题。那是

我的服务在一段时间后终止。我无法检查其原因。请帮忙。

这是一些代码片段

@Override
public void onCreate() {
    super.onCreate();
    timer.schedule(checkLocationListener, new Date(), 1000 * 60 * 15);
}

private TimerTask checkLocationListener = new TimerTask() {
    @Override
    public void run() {
        handler.post(new Runnable() {
            public void run() {
                lm.removeUpdates(LocationService.this);
  lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, CommonConstants.GPS_POLL,
                    CommonConstants.GPS_MIN_DIST, LocationService.this);
            }
        });
    }
};



@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(LocationService.class.getSimpleName(), "Start");
    valueMap.put(CommonConstants.STATUS, CommonConstants.STATUS_DROP);
    trackMe();
    new Thread() {
        public void run() {
            while (true) {
                try {
                    String url = DeviceConfig.getMessage();
                    if (url == null) {
                        Thread.sleep(60000);
                        continue;
                    }
                    HttpRequester.getServerResponse(url, null);
                    DeviceConfig.addValue(CommonConstants.SERVER_UPD, "" + System.currentTimeMillis());
                    DeviceConfig.removeMessage();
                } catch (Exception e) {
                    Log.e(LocationService.class.getSimpleName(), "Error", e);
                }
            }
        }
    }.start();
    return (START_NOT_STICKY);
}

private void trackMe() {
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, CommonConstants.GPS_POLL, CommonConstants.GPS_MIN_DIST,
        this);
    DeviceConfig.addValue(CommonConstants.GPS_STATE, lm.isProviderEnabled(LocationManager.GPS_PROVIDER)
        ? CommonConstants.ON : CommonConstants.OFF);
}

I have created a GPS service which reads location updates on a regular interval. Earlier there was an issue that GPS was going in sleep mode after some time and no location updates were giver thereafter.

Now I have changed my code to unregister and register GPS location updates on a regular interval. This solves my problem of GPS going in sleep mode but a new issue is created. That is

My service is terminated after some time. I am not able to check the reason for this. Please Help.

Here are some code snipplets

@Override
public void onCreate() {
    super.onCreate();
    timer.schedule(checkLocationListener, new Date(), 1000 * 60 * 15);
}

private TimerTask checkLocationListener = new TimerTask() {
    @Override
    public void run() {
        handler.post(new Runnable() {
            public void run() {
                lm.removeUpdates(LocationService.this);
  lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, CommonConstants.GPS_POLL,
                    CommonConstants.GPS_MIN_DIST, LocationService.this);
            }
        });
    }
};



@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(LocationService.class.getSimpleName(), "Start");
    valueMap.put(CommonConstants.STATUS, CommonConstants.STATUS_DROP);
    trackMe();
    new Thread() {
        public void run() {
            while (true) {
                try {
                    String url = DeviceConfig.getMessage();
                    if (url == null) {
                        Thread.sleep(60000);
                        continue;
                    }
                    HttpRequester.getServerResponse(url, null);
                    DeviceConfig.addValue(CommonConstants.SERVER_UPD, "" + System.currentTimeMillis());
                    DeviceConfig.removeMessage();
                } catch (Exception e) {
                    Log.e(LocationService.class.getSimpleName(), "Error", e);
                }
            }
        }
    }.start();
    return (START_NOT_STICKY);
}

private void trackMe() {
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, CommonConstants.GPS_POLL, CommonConstants.GPS_MIN_DIST,
        this);
    DeviceConfig.addValue(CommonConstants.GPS_STATE, lm.isProviderEnabled(LocationManager.GPS_PROVIDER)
        ? CommonConstants.ON : CommonConstants.OFF);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫弦 2025-01-15 15:43:52

您不需要一次又一次地注册/取消注册位置更新侦听器,看起来您的服务已停止,因此以粘性模式启动您的服务,这样,只能显式停止服务。

You wont need to register/unregister location update listener again and again, It seems your service stopped, so start your service in sticky mode, in that way, service can be stopped explicitly only.

白首有我共你 2025-01-15 15:43:52

如果您确实希望服务继续运行,则需要将其设为前台服务(请参阅 http://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification))

但为什么不使用这个 http://developer.android.com/reference/android/location/LocationManager .html#requestLocationUpdates(long, float, android.location.Criteria, android.app.PendingIntent) 每次位置更改时启动您的服务

Phil ?

If you really want your service to keep running you need to make it a foreground service (see http://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification))

But why not use this http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(long, float, android.location.Criteria, android.app.PendingIntent) to start your service each time the location changes?

Phil

不可一世的女人 2025-01-15 15:43:52

到目前为止我正在这样做。

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
   CommonConstants.GPS_POLL,CommonConstants.GPS_MIN_DIST, pendingIntent);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
   CommonConstants.NW_POLL, CommonConstants.NW_MIN_DIST, pendingIntent);

请建议您是否有更好的解决方案。

SO far I am doing this.

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
   CommonConstants.GPS_POLL,CommonConstants.GPS_MIN_DIST, pendingIntent);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
   CommonConstants.NW_POLL, CommonConstants.NW_MIN_DIST, pendingIntent);

Please suggest if you have some better solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文