我正在尝试创建一个允许用户记录路线(位置/GPS)的应用。为了确保即使屏幕关闭也能记录位置,我为位置记录创建了一个前台服务。我将位置存储在 Room Database 中,该数据库使用 Dagger2 注入(inject)到我的服务中。
但是,这个服务被 Android 杀死了,这当然不好。我可以订阅内存不足警告,但这并不能解决我的服务在运行 Android 8.0 的现代高端手机上大约 30 分钟后被终止的根本问题
我创建了一个只有“Hello world” Activity 和服务的最小项目:https://github.com/RandomStuffAndCode/AndroidForegroundService
服务是在我的Application类中启动的,路由日志是通过一个Binder启动的:
// Application
@Override
public void onCreate() {
super.onCreate();
mComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
Intent startBackgroundIntent = new Intent();
startBackgroundIntent.setClass(this, LocationService.class);
startService(startBackgroundIntent);
}
// Binding activity
bindService(new Intent(this, LocationService.class), mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT);
// mConnection starts the route logging through `Binder` once connected. The binder calls startForeground()
我可能不需要 BIND_AUTO_CREATE 标志,我一直在测试不同的标志以试图不让我的服务被杀死 - 到目前为止还没有运气。
使用分析器,我似乎没有任何内存泄漏,内存使用量稳定在 ~35mb:
使用 adb shell dumpsys activity processes > tmp.txt 我可以确认 foregroundServices=true 并且我的服务在 LRU 列表中排名第 8:
Proc#3: prcp F/S/FGS trm: 0 31592:com.example.foregroundserviceexample/u0a93 (fg-service)
似乎不可能创建一个您可以信任不会被杀死的前台服务。所以,我们能做些什么?嗯……
START_STICKY 启动服务。这似乎有点浪费,并且不会导致非常漂亮的代码,但它可能会工作......有点。根据 Android 在杀死服务后重新创建服务所需的时间,大部分位置可能会丢失。这真的是目前在 Android 上后台做事的状态吗?没有更好的办法吗?
编辑:将应用程序列入白名单以进行电池优化(禁用它)不会阻止我的服务被终止
编辑:使用 Context.startForegroundService() 启动服务并没有改善这种情况
编辑:所以这确实只发生在 一些 设备上,但它始终在它们上发生。我想您必须做出选择,要么不支持大量用户,要么编写非常丑陋的代码。太棒了。
最佳答案
由startForeground 启动的服务属于第二重要的组visible process :
A visible process is doing work that the user is currently aware of, so killing it would have a noticeable negative impact on the user experience. A process is considered visible in the following conditions:
It is running an Activity that is visible to the user on-screen but not in the foreground (its onPause() method has been called). This may occur, for example, if the foreground Activity is displayed as a dialog that allows the previous Activity to be seen behind it.
It has a Service that is running as a foreground service, through Service.startForeground() (which is asking the system to treat the service as something the user is aware of, or essentially visible to them).
It is hosting a service that the system is using for a particular feature that the user is aware, such as a live wallpaper, input method service, etc.
The number of these processes running in the system is less bounded than foreground processes, but still relatively controlled. These processes are considered extremely important and will not be killed unless doing so is required to keep all foreground processes running.
话虽如此,您永远无法确定您的服务在任何时候都不会被终止。例如。内存压力、电池电量低等。见 who-lives-and-who-dies .
关于如何处理,基本上你自己回答了这个问题。要走的路是START_STICKY :
For started services, there are two additional major modes of operation they can decide to run in, depending on the value they return from
onStartCommand():START_STICKYis used for services that are explicitly started and stopped as needed, whileSTART_NOT_STICKYorSTART_REDELIVER_INTENTare used for services that should only remain running while processing any commands sent to them. See the linked documentation for more detail on the semantics.
作为一般准则,您应该尽可能少地在后台(或前台)服务中执行操作,即只进行位置跟踪并将其他所有内容保留在前台 Activity 中。只有跟踪需要很少的配置并且可以快速加载。此外,您的服务越小,它被杀死的可能性就越小。只要它没有被杀死,系统就会将您的 Activity 恢复到进入后台之前的状态。另一方面,前台 Activity 的“冷启动”应该不是问题。
我不认为这是丑陋的,因为这可以保证手机始终为用户提供最佳体验。这是它必须做的最重要的事情。不幸的是,某些设备会在 30 分钟后关闭服务(可能没有用户交互)。
所以,正如你所说,你必须
Persist everything in the service in e.g. a Room database. Every variable, every custom class, every time any of them changes and then start the service with START_STICKY.
见 creating a never ending service
隐含问题:
Depending on how long it takes for Android to re-create the service after killing it, a large portion of locations may be lost.
这通常只需要很短的时间。特别是因为您可以使用 Fused Location Provider Api用于位置更新,这是一个独立的系统服务,不太可能被杀死。所以主要看你需要在onStartCommand中重新创建服务的时间。
Also take note that from Android 8.0 onwards you need to use a
forground servicebecause of the background location limits.
编辑: 正如最近在新闻中报道的那样: 一些制造商可能会让您难以保持服务运行。本站https://dontkillmyapp.com/跟踪制造商和您设备的可能缓解措施。 Oneplus目前 (29.01.19) 是最严重的违规者之一。
When releasing their 1+5 and 1+6 phones, OnePlus introduced one of the most severe background limits on the market to date, dwarfing even those performed by Xiaomi or Huawei. Not only did users need to enable extra settings to make their apps work properly, but those settings even get reset with firmware update so that apps break again and users are required to re-enable those settings on a regular basis.
Solution for users
Turn off System Settings > Apps > Gear Icon > Special Access > Battery Optimization.
可惜有
No known solution on the developer end
关于android - 在高端手机上杀死最小的android前台服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49637967/
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru
在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo
我想在Ruby中创建一个用于开发目的的极其简单的Web服务器(不,不想使用现成的解决方案)。代码如下:#!/usr/bin/rubyrequire'socket'server=TCPServer.new('127.0.0.1',8080)whileconnection=server.acceptheaders=[]length=0whileline=connection.getsheaders想法是从命令行运行这个脚本,提供另一个脚本,它将在其标准输入上获取请求,并在其标准输出上返回完整的响应。到目前为止一切顺利,但事实证明这真的很脆弱,因为它在第二个请求上中断并出现错误:/usr/b
您如何在Rails中的实时服务器上进行有效调试,无论是在测试版/生产服务器上?我试过直接在服务器上修改文件,然后重启应用,但是修改好像没有生效,或者需要很长时间(缓存?)我也试过在本地做“脚本/服务器生产”,但是那很慢另一种选择是编码和部署,但效率很低。有人对他们如何有效地做到这一点有任何见解吗? 最佳答案 我会回答你的问题,即使我不同意这种热修补服务器代码的方式:)首先,你真的确定你已经重启了服务器吗?您可以通过跟踪日志文件来检查它。您更改的代码显示的View可能会被缓存。缓存页面位于tmp/cache文件夹下。您可以尝试手动删除
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
假设我有以下类(class):classPersondefinitialize(name,age)@name=name@age=ageenddefget_agereturn@ageendend我有一组Person对象。是否有一种简洁的、类似于Ruby的方法来获取最小(或最大)年龄的人?如何根据它对它们进行排序? 最佳答案 这样做会:people_array.min_by(&:get_age)people_array.max_by(&:get_age)people_array.sort_by(&:get_age)
require"socket"server="irc.rizon.net"port="6667"nick="RubyIRCBot"channel="#0x40"s=TCPSocket.open(server,port)s.print("USERTesting",0)s.print("NICK#{nick}",0)s.print("JOIN#{channel}",0)这个IRC机器人没有连接到IRC服务器,我做错了什么? 最佳答案 失败并显示此消息::irc.shakeababy.net461*USER:Notenoughparame
我有一个使用PDFKit呈现网页的pdf版本的Rails应用程序。我使用Thin作为开发服务器。问题是当我处于开发模式时。当我使用“bundleexecrailss”启动我的服务器并尝试呈现任何PDF时,整个过程会陷入僵局,因为当您呈现PDF时,会向服务器请求一些额外的资源,如图像和css,看起来只有一个线程.如何配置Rails开发服务器以运行多个工作线程?非常感谢。 最佳答案 我找到的最简单的解决方案是unicorn.geminstallunicorn创建一个unicorn.conf:worker_processes3然后使用它: