简体   繁体   English

无限期运行Android服务

[英]Running Android service for infinite time

I have a location service that runs in the background and uses the location manager to send lat and long values to my server every 30 seconds. 我有一个在后台运行的位置服务,并使用位置管理器每30秒将经纬度和经度值发送到我的服务器。 I want this service to run infinitely long until the user stops the service. 我希望此服务无限长地运行,直到用户停止该服务为止。 But what i am observing is after few hours, the service vanishes as it reaches more than 30+ mb. 但是我观察到的是几个小时后,服务超过30 MB时消失了。 I want to know how I can make it run as long as it is not stopped by the user? 我想知道如何让它在用户不停止的情况下运行?

Few of my observations(correct me if I am wrong): In the allocation tracker, most of the objects that occupy the memory are the location manager objects. 我的观察很少(如果我做错了,请纠正我):在分配跟踪器中,占据内存的大多数对象都是位置管理器对象。 In the heap, when i cause gc, the memory allocated by my objects are vanishing and so i guess there is no memory leak. 在堆中,当我导致gc时,由我的对象分配的内存消失了,因此我猜没有内存泄漏。 In the applications-->running services, i do not see any services running for 4+ hours. 在应用程序->运行服务中,我看不到任何运行4小时以上的服务。 So What I am trying to do is not possible? 所以我想做的是不可能的吗?

Any help/suggestions are greatly appreciated. 任何帮助/建议,我们将不胜感激。

Thanks in advance. 提前致谢。

I'm not sure why your service starts to take up more than 30 megabytes, but it is possible you are leaking memory somehow. 我不确定为什么您的服务开始占用超过30兆字节的空间,但是您可能会以某种方式泄漏内存。

In the end though, your design is flawed.The best thing you can do is: 最后,您的设计有缺陷,您可以做的最好的事情是:

  1. Use the PendingIntent method registerLocationUpdates to register for location updates. 使用PendingIntent方法registerLocationUpdates注册位置更新。 You can specify the minTime between updates here. 您可以在此处指定两次更新之间的分钟时间。
  2. If you need send the position updates precisely every 30 seconds, you can also register a PendingIntent with the AlarmManager to have an intent get sent every 30 seconds. 如果需要每30秒精确发送一次位置更新,则还可以向AlarmManager注册PendingIntent,以每30秒发送一次意图。
  3. Have this PendingIntent start an IntentService. 让此PendingIntent启动IntentService。
  4. Have the IntentService send the location data to your server. 让IntentService将位置数据发送到您的服务器。
  5. When the user stops your application, simply unregister your PendingIntents with the LocationManager (and potentially AlarmManager). 当用户停止您的应用程序时,只需向LocationManager(可能还有AlarmManager)注销您的PendingIntents。

I have a location service that runs in the background and uses the location manager to send lat and long values to my server every 30 seconds. 我有一个在后台运行的位置服务,并使用位置管理器每30秒将经纬度和经度值发送到我的服务器。

Please allow the user to choose the polling period, including "never poll". 请允许用户选择轮询周期,包括“从不轮询”。

Also, please realize that this will seriously impact the user's battery. 另外,请意识到这将严重影响用户的电池。

I want this service to run infinitely long until the user stops the service. 我希望此服务无限长地运行,直到用户停止该服务为止。

This is not possible. 这是不可能的。 The closest you can get is via startForeground() , but not even that guarantees that your service will live forever. 您可以通过startForeground()获得最接近的结果,但这甚至不能保证您的服务将永远存在。

Moreover, this is a serious anti-pattern in Android. 此外,这是Android中的一种严重的反模式。 Users hate applications that try to run forever, which is why we have to contend with task killers and the like. 用户讨厌尝试永远运行的应用程序,这就是为什么我们不得不与任务杀手等竞争的原因。

But what i am observing is after few hours, the service vanishes as it reaches more than 30+ mb. 但是我观察到的是几个小时后,服务超过30 MB时消失了。

In a few hours of keeping the GPS on and the device awake all of the time, your user's battery will be dead, at which point your service and everything else vanishes. 在保持GPS始终处于打开状态且设备始终处于唤醒状态的几个小时内,用户的电池电量将耗尽,这时您的服务及其他所有功能都会消失。

With respect to the memory, if you think you are leaking memory, use MAT to track down the leaks. 关于内存,如果您认为自己正在泄漏内存,请使用MAT跟踪泄漏情况。

Why don't you issue gc programatically - say every hour? 您为什么不以编程方式发布gc-每小时说一次?

I don't know if it is impossible what you want to do. 我不知道你想做什么是不可能的。 I guess it should be possible after all. 我想毕竟应该有可能。 What makes me wonder is: why doesn't the VM start gc and does instead crash? 让我感到奇怪的是:为什么VM不启动gc而是崩溃?

Maybe there is in fact an issue with memory leakage? 也许实际上是内存泄漏问题?

Avoiding memory leaks -blogentry can give some hints, maybe. 避免内存泄漏-也许可以给一些提示。

Perhaps someone can give you more sophisticated advice if you give us some code of yours? 如果您给我们一些您的代码,也许有人可以给您更复杂的建议?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM