Creating a Service
As noted previously, Android services are for long‑running processes that may need to keep running even when decoupled from any activity. Examples include playing music even if the “player” activity gets garbage‑collected, polling the Internet for RSS/Atom feed updates, and maintaining an online chat connection even if the chat client loses focus due to an incoming phone call.
Services are created when manually started (via an API call) or when some activity tries connecting to the service via inter‑process communication (IPC). Services will live until no longer needed and if RAM needs to be reclaimed. Running for a long time isn’t without its costs, though, so services need to be careful not to use too much CPU or keep radios active much of the time, or else the service causes the device’s battery to get used up too quickly.
This chapter covers how you can create your own services; the next chapter covers how you can use such services from your activities or other contexts. Both chapters will analyze the sample application, with this chapter focusing mostly on the implementation. extends the weather‑fetching logic of the original sample, by bundling it in a service that monitors changes in location, so the weather is updated as the emulator is “moved”.
Дата добавления: 2015-05-16; просмотров: 801;