这是一个后台服务激活锁定或startForeground更好 [英] Which is better for a background service WakeLock or startForeground

查看:199
本文介绍了这是一个后台服务激活锁定或startForeground更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

诚然,我只是那种黑客在这里,所以我想一些知识。

我有我连接到倒计时定时器另一个线程在后台运行的服务。我是有倒计数垂死的问题,经过一段时间和presumed这是由于该服务的垃圾收集。我似乎使用startForeground已经解决了的问题(看到没有真正的电池使用)。现在,我读到wakelocks,有没有关于何时使用一个或任何的最佳做法其他?

谢谢!


解决方案

  

我有我连接到倒计时定时器另一个线程在后台运行的服务。


请使用 AlarmManager ,所以你的服务只需要在内存中它实际上是在做工作的时候,不只是看时钟滴答。一个关于Android的最大用户投诉的是所有开发人员创建阻塞自己的手机这些后台的东西。


  

我似乎使用startForeground已经解决了的问题(看没有真正使用电池)。


背后点startForeground()是表明你的服务不断将价值传递给用户,例如,如果该服务被回收,由于挂用户会发现各地过长或内存不足的情况​​。坐着看时钟嘀哒的的持续提供价值给用户。请使用 AlarmManager ,你将不再需要 startForeground()


  

现在我读到wakelocks,有没有关于何时使用一个或任何的最佳做法其他?


激活锁定保持供电的CPU(可能还有其他的东西,如屏幕)。它有一点做与 startForeground()。如果你使用 AlarmManager ,这取决于你正在做的工作,你可能需要一个激活锁定来保证设备住宿清醒而你做你的perodic WO

Admittedly I am just kind of hacking here so I would like some knowledge.

I have a service I run in the background connected to another thread that counts down a timer. I was having problems with the count down dying after a while and presumed it was due to garbage collection of the service. I seem to have fixed the issue (and see no real battery use) using startForeground. Now I read about wakelocks, are there any best practices on when to use one or the other?

Thanks!

解决方案

I have a service I run in the background connected to another thread that counts down a timer.

Please use AlarmManager, so your service only needs to be in memory when it is actually doing work, not just watching the clock tick. One of the biggest user complaints about Android is all these background things that developers create that clog up their phones.

I seem to have fixed the issue (and see no real battery use) using startForeground.

The point behind startForeground() is to indicate that your service is continuously delivering value to the user, such that the user will notice if the service is reclaimed due to hanging around too long or low memory conditions. Sitting and watching the clock tick is not "continuously delivering value to the user". Please use AlarmManager, and you will not need startForeground().

Now I read about wakelocks, are there any best practices on when to use one or the other?

WakeLock keeps the CPU powered on (and possibly other things, like the screen). It has little to do with startForeground(). If you use AlarmManager, depending on the work that you are doing, you may need a WakeLock to ensure the device stays awake while you do your perodic wo

这篇关于这是一个后台服务激活锁定或startForeground更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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