A (175)

(Tuis.) #1

486 CHAPTER 13: Android Service Class and Threads: Background Processing



  1. Name the Service subclass AmbientService and click the Browse button
    next to the Superclass field to open the Superclass Selection dialog. Type
    in the letters “servi” to get a list of all the Service classes.

  2. Double-click on the Service - android.app superclass option, at the top of
    the dialog, to select it as the Android superclass that you want to extend.

  3. Once you are back in the New Java Class dialog, select the
    absolute.beginners.hellouniverse package in the Package field. Use the
    Browse button to access the selector dialog to find the package.

  4. Leave all of the other New Java Class dialog settings at their defaults and
    then click the Finish button to create your bootstrap AmbientService
    Service subclass, which is shown in Figure 13-2. Notice that the
    import statements for the android.app.Service package and the
    android.content.Intent package are written for you.


Figure 13-2. The bootstrap AmbientService Service subclass with required IBinder( ) method and imports


Both of these import statements are needed because Intent objects are used to launch Service
objects. There is also an android.os.IBinder package import statement included, because the
public IBinder onBind( ) method is required to be implemented inside any Service subclass. Since
you are going to be creating a started Service and not a bound Service, you will be leaving this
.onBind( ) method empty and unutilized, which is allowed, as long as it is there (as long as it exists in
your Java code base).



  1. If you want, you can delete the comment in the method that says “TODO
    Auto-generated method stub.” When you do so, the to-do checkbox icon
    shown in the left margin of Eclipse in Figure 13-2, will disappear. Eclipse
    is telling you that you need to write code for the inside of this method,
    but unless you are creating a bind between your application logic and the
    Service, you don’t really need to. Still, Eclipse is pretty cool, isn’t it?

Free download pdf