有哪些方法可以优化 Android 上运行的服务?
我想知道您可以以编程方式执行哪些操作来最大限度地减少服务在设备上的占用空间?编写服务时是否有任何特殊技巧,可以使其不占用太多系统内存?我想低内存占用是我主要关心的问题,因此用户不想关闭服务并愿意让它始终运行。
***编辑***
好吧,阅读答案我想我必须这样做错误。我使用 AlarmManager 定期唤醒服务,但除非用户通过主活动指示,否则我不会停止服务,所以我应该在服务执行所需操作后在 onStartCommand 末尾包含此内容。调用 stopService? 不会停止服务调用 onDestroy 因为如果它停止我正在 onDestroy 中取消注册我的 AlarmManager。
当我在手机上运行服务时,它正在运行,但它实际上并没有做任何事情,直到 AlarmManager 关闭,此时它执行它的小功能,这就是。它。
I'm wondering what you can do programmatically to minimize the footprint that your service will have on a device? Are there any particular tricks to the way that you write a service so that it doesn't take up much system memory? I guess low memory footprint is my main concern so that a user does not want to turn the service off and is willing to have it always running.
***EDIT***
Okay so reading the answers I"m thinking that I must be doing this wrong. I am using the AlarmManager to periodically wake the service up but I am not ever stopping the service unless the user indicates via the main activity. So should I include at the end of my onStartCommand after my service performs what it needs to should I call stopService? Doesn't stop service call onDestroy because if it does I was deregistering my AlarmManager in onDestroy.
The way that it runs right now when I go to running services on my phone it has the service running but it is not actually doing anything until the AlarmManager goes off at which point it performs it's small function and that's it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个巨大的优化空间。 Android 服务并不意味着一直运行(如 Windows 服务或 unix 守护进程)。
它们更像是一个任务解决器,例如下载文件。完成任务后,他们应该调用 Service.stopSelf()。
如果您的应用程序再次需要该服务,则应重新启动该服务以执行适当的任务。
Theres a huge one for optimization. Androids services are not meant to run all the time (like Windows services or unix daemons).
They are more like a task solver, e.g. download a file. After finished with their task, they should call Service.stopSelf().
If your app needs the service again, it should restart it then for the appropriate task.
这实际上取决于您的服务正在做什么,因此实际上没有正确或错误的答案。我要注意的重要一点是,不要让服务的运行时间超过所需的时间。如果您的服务大部分时间处于空闲状态,请考虑通过警报定期唤醒它,并在其定期工作完成后停止服务。这样做的另一个好处是,如果服务运行时间不长,可以防止许多任务杀手破坏您的服务。
我还尝试使用不精确的警报,因为这些警报通常对电池更友好,并且电池寿命也是您应该注意的事情。
就内存占用而言,始终值得释放任何未使用的资源,并尽可能保持代码简洁,无论是在服务、活动、接收器还是其他任何地方。尽管当前的智能手机比功能手机拥有更多的内存,但 Android 越来越多地在规格相对较低的硬件上运行,因此值得关注低端设备。
Reto Meier 最近在他的博客上撰写了一些涵盖此类主题的文章。
It really depends on what your service is doing so there's really no right or wrong answer. The important thing that I try to stay mindful of is not to keep the service alive for any longer than is required. If your service spends most of its time in an idle state, then consider waking it periodically with an alarm, and stopping the service once its periodic work is complete. This has the added benefit of preventing many task killers from destroying your service if it doesn't stay running for very long.
I also try and use inexact alarm as these are generally kinder on batteries, and battery life is also something that you should be mindful of.
In terms of memory footprint, it is always worth freeing up any unused resources, and keeping your code as lean and mean as possible, whether it is in a Service, Activity, Receiver, or anywhere else. Although current smart phones have considerably more memory than feature phones, Android is increasingly being run on comparatively lower spec hardware, so it's worth being mindful of lower end devices.
Reto Meier has recently written some articles which cover these sorts of topics on his blog.