|
Pro Android with Kotlin@de android telegram Pro Android with Kotlin Developing Modern Mobile35
CHAPTER 4: Services
The previous code starts the service automatically once you bind to it and it doesn’t exist
yet. This happens by virtue of this statement:
val flags = BIND_AUTO_CREATE
[...]
If you don’t need it because you are sure the service is running, you can omit it. In most
cases, it is however better to include that flag. The following are the other flags you can use
for setting binding characteristics:
BIND_AUTO_CREATE
: We just used that; it means the service gets started
automatically if it hasn’t started yet. You’ll sometimes read that explicitly
starting a service is unnecessary if you bind to it, but this is true only if
you set this flag.
BIND_DEBUG_UNBIND
: This leads to saving the callstack of a following
unbindService()
, just in case subsequent unbind commands are
wrong. If this happens, a more verbose diagnostic output will be shown.
Since this imposes a memory leak, this feature should be used only for
debugging purposes.
BIND_NOT_FOREGROUND
: This is applicable only if the client runs in a
foreground process and the target service runs in a background
process. With this flag, the binding process will not raise the service to a
foreground scheduling priority.
BIND_ABOVE_CLIENT
: With this flag, we specify that the service is more
important than the client (i.e., service invoker). In case of a resource
shortage, the system will kill the client prior to the service invoked.
BIND_ALLOW_OOM_MANAGEMENT
: This flags tell the Android OS that you are
willing to accept Android treating the binding as noncritical and killing
the service under low memory circumstances.
BIND_WAIVE_PRIORITY
: This flags leads to leaving the scheduling of the
service invocation up to the process where the service runs in.
Just add them in a combination that suits your needs.
Note
Binding is not possible from inside a
BroadcastReceiver
component, unless
the
BroadcastReceiver
has been registered via
registerReceiver(receiver.
intentfilter)
. In the latter case, the lifetime of the receiver is tied to the registering component.
You can, however, from broadcast receivers pass instruction strings inside the intent you used for
starting (in other words, not binding) the service.
|
|
|