Android的服务:绑定或启动? [英] Android Service : bind or start?

查看:128
本文介绍了Android的服务:绑定或启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在什么情况下,我应该开始服务以及在什么情况下绑定服务?例如 - 一个Android客户端的音乐服务?是在优先系统的任何差异,是任何共同的规则;什么事吗?

In what cases should I start Service and in what case bind Service? For example - an android client for Music Service? Are the any differences in the priority for the System;are the any common rules; anything else?

推荐答案

使用 startService()为将独立运行开始后,他们的服务。音乐播放器是一个很好的例子。这些运行,直到他们叫 stopSelf()或有人叫 stopService()

Use startService() for services which will run independently after you start them. Music players are a good example. These run until they call stopSelf() or someone calls stopService().

您可以与正在运行的服务通过发送意图来回沟通,但在大多数情况下,你只需启动该服务,并让它自己运行。

You can communicate with a running service by sending Intents back and forth, but for the most part, you just start the service and let it run on its own.

使用绑定()当服务端和客户端将通信的持久连接来回。一个很好的例子是一个导航服务将被发送位置更新返回给客户端。粘合剂不少难度比意图写的,但他们是真正的路要走这种使用情况。

Use bind() when the service and client will be communicating back and forth over a persistent connection. A good example is a navigation service which will be transmitting location updates back to the client. Binders are a lot harder to write than intents, but they're really the way to go for this usage case.

关于优先级:当一个进程的所有活动失去了自己的知名度,这个过程就变成了的服务流程的,如果它承载这是开始 ONSTART()服务,否则就变成了的后台进程的。服务流程具有的优先级高于后台进程。在 Android开发者网站进一步的细节。

Regarding the priority: When all activities of a process lose their visibility, the process becomes a service process if it hosts a service which was started with onStart(), otherwise it becomes a background process. Service processes have a higher priority than background processes. Further details at the android developer site.

如果没有明显的活动服务流程需要更高的优先级(例如,一个音乐播放器,实在不应该被打断),该服务可以调用 startForeground()

If a service process without visible activity needs a higher priority (e.g. a music player which really should not be interrupted), the service can call startForeground().

这篇关于Android的服务:绑定或启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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