简体   繁体   English

Android本地服务:它们有用吗?

[英]Android local services : are they that useful?

I don't see the point of using a local service in Android. 我看不到在Android中使用本地服务的意义。 If I want to do backgound stuff, I can create a thread and use Handlers. 如果我想做背景材料,我可以创建一个线程并使用处理程序。

Creating a local service is a big headache, you have to mess with Binders, worry about the start/stop/bind/unbind lifecycle, etc. 创建本地服务是一件令人头疼的事情,您必须搞砸Binders,担心启动/停止/绑定/取消绑定生命周期等。

What does a local service get me that a thread doesn't ? 线程不提供的本地服务让我得到什么?

With a Service you can respond to system broadcasts and carry out some action in the background without the user having to start your app. 借助Service您可以响应系统广播并在后台执行某些操作,而无需用户启动您的应用程序。 For example, Listen will periodically download podcasts in the background without having to manually start the app. 例如,“收听”将在后台定期下载播客,而无需手动启动该应用程序。

If you want to run things in background without the onpause effect you do it by a service. 如果您想在后台运行事物而没有暂停效果,则可以通过服务来完成。 For example if you want to play music, if you do that on the activity level once the user leaves the activity it will stop as the activity goes to pause, and eventually the system will kill it later. 例如,如果您想播放音乐,如果您在活动级别执行此操作,则一旦用户离开活动,它将在活动暂停时停止,最终系统将在以后将其杀死。 Services too can be killed. 服务也可以被杀死。

Another example is if you want to retrieve for example the weather info, you would not start an activity for such thing as the user doesn't need to see an UI for a scheduled weather update progress. 另一个示例是,例如,如果您要检索天气信息,则不会为用户启动活动,因为用户不需要查看计划的天气更新进度的UI。

For now just loose the bind stuff behind Services, they work without those too very well. 目前,只需松开Services背后的绑定内容,它们就可以很好地工作。

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

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