A (175)

(Tuis.) #1
CHAPTER 13: Android Service Class and Threads: Background Processing 497


  1. If you click the Add unimplemented methods option, Eclipse will write your
    “bootstrap” onClick( ) method code block (structure) for you. Once that is in
    place, you will be ready to add your .startService( ) method call, which will
    utilize an Intent object to start your AmbientService Service subclass. That
    will then start the Android MediaPlayer and start looping your ambient.m4a
    digital audio asset, which will then play your background audio.

  2. Inside of the bootstrap onClick( ) method Eclipse created for you, which is
    shown in Figure 13-14, type in your startService( ) method call. Inside of that
    method call, use the Java new keyword to create an Intent object using the
    Intent( ) constructor method and reference to the current Context and your
    AmbientService class.


Figure 13-14. Add startService( ) method call with a new Intent( ) method call inside it referencing AmbientService


The onClick( ) event handling structure should look like this when you are done, as shown in
Figure 13-14:


@Override
public void onClick(View arg0) {
startService( new Intent(this, AmbientService.class) );
}



  1. As you can see in Figure 13-15, the code should be error-free; however, it
    isn’t! Eclipse is showing a wavy red underline underneath the entire Java
    construct that you created inside the startService( ) method call. Mouseover
    the error and read the error message in the first line of the pop-up helper
    dialog. It reads “The constructor Intent(new View.OnClickListener( ),
    Class) is undefined.”

Free download pdf