📜 ⬆️ ⬇️

The book "Head First. Programming for Android. 2nd ed »

image Hello! We recently published the updated work of David Griffiths and Don Griffiths. We offer to get acquainted with the 19th chapter "Related services"

Running services are great for background operations - and if you need a service with more interactivity? In this chapter, you will learn how to create related services — a type of service with which your activities can interact. You will learn how to bind to a service and how to cancel it after you finish your work to save resources. The Android Positioning Service helps you get location information from your device’s GPS receiver. Finally, you will learn how to use the Android permissions model, including processing permission requests at run time.

Related services are bound to other components.


As mentioned in Chapter 18, the service being started starts when the intent is transmitted. It runs the code in the background and stops when the operation completes. Such services continue to run even if the component that launches them is destroyed.

A linked service binds to another component — for example, an activity. Unlike services being started, a component can interact with the associated service and call its methods.
')
To see how the related services work, we will create a new application with an associated service, which, like an odometer, measures the distance traveled by the machine. The Android positioning service will be used to measure the distance:

image

What are we going to do


The process of building the application will consist of three basic steps:
image

Creating an Odometer Project

Start by creating a project. Create a new Android project with the name “Odometer”, the domain of the company “hfad.com” and the package name com.hfad.odometer. The minimum level of the SDK should be equal to API 19 for the application to work on most devices. Create an empty activity with the name “MainActivity” and a layout with the name “activity_main” so that your code does not differ from ours. Be sure to uncheck the Backwards Compatibility (AppCompat) checkbox when creating activity.
image

This code implements a single onBind () method, which is called when a component (for example, an activity) issues a request to bind to a service. The method takes one parameter Intent and returns an IBinder object. The IBinder interface is used to associate a service with activity; You must provide its implementation in the code of your service. Now you will learn how to do it.

IBinder implementation


To implement IBinder, add a new inner class to the service code that extends the Binder class (which implements the IBinder interface). The inner class must include the method used by the activity to get the link to the associated service.

We define the OdometerBinder class, which is used by the MainActivity to get a reference to the OdometerService. Its definition code looks like this:
image

We wrote all the service code needed to associate a MainActivity with an OdometerService.
Now we will add to the service a new method with which it will return a random number.

Add getDistance () method


A code called getDistance () will be added to the OdometerService class, which will be called by our activity. For now, it will return a random number, and later we will update it so that it uses the Android positioning service.

Below is the complete OdometerService.java code; add to your version
Changes in bold:
image

Now we’ll update the MainActivity class to use OdometerService.

MainActivity layout update


The next step in building the application is to associate the MainActivity with the OdometerService service and call its getDistance () method. Start by adding the label to the layout.
MainActivity. It will display the number returned by the getDistance () method of the OdometerService class.

Change your version of the markup activity_main.xml and make the changes in bold:
image


»More information about the book can be found on the publisher's website.
» Announcement
» Excerpt

For Habrozhiteley 20% coupon discount - Head First

Source: https://habr.com/ru/post/359072/


All Articles