如何在后台在Android中运行服务24x7以获取用户位置 [英] How to run service 24x7 in android in background to get location of user

查看:224
本文介绍了如何在后台在Android中运行服务24x7以获取用户位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了多种不同的方式来运行后台服务来获取用户的位置,但是android系统会在一段时间后自动终止服务. 虽然它可以在某些设备上运行,但不能在大多数设备上运行. 我正在构建类似于UBER的应用程序.我希望在一段时间后更新驱动程序的位置,即使应用程序处于后台或前景,直到用户未将状态设置为离线

I have tried so many different ways to run background service to get location of user but android system automatically kills service after some time. Though it works on some devices but not working on most of the devices. I am building app similar to UBER. I want driver's location to be updated after some interval of time even if app is in background or Foreground until user does not set status to offline

推荐答案

您可以使用前台服务,这是一项普通的服务,在前台有正在进行的通知,可停止操作系统停止/终止您的服务进程.

You can use Foreground Service, which is just a normal service with an ongoing notification in the foreground, which stops the OS to stop/kill your service process.

顺便说一句,这不能保证当设备进入打ze或App Standby模式时,您的服务将获得CPU/处理时间.

By the way, this does not gurantee that your service will be give CPU/processing time when device goes into Doze or App Standby modes.

尽管您不能绕过这些打ze,待机和电池优化,但是我已经测试了一种通过在前台服务中创建唤醒锁并在单独的进程中启动该服务来避免这些问题的方法.

Although you cannot bypass these doze, standby and battery optimizations, but I've tested a hack to avoid these by creating a wake lock in your foreground service and starting that service in a separate process.

希望有帮助.

这篇关于如何在后台在Android中运行服务24x7以获取用户位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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