A (175)

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


  1. Next, add a line of space after the setContentView( ) method call in your
    PlayVideo class’s onCreate( ) method and instantiate your soundOn and
    soundOffImageView objects by using the following two lines of Java code:


soundOn = (ImageView)findViewById(R.id.playAudio);
soundOff = (ImageView)findViewById(R.id.stopAudio);

Now that you have declared, instantiated, and loaded your two ImageView objects with references
to your XML definitions, which in turn reference your digital image assets as their source imagery,
you are ready to add event handling structures to each of these to “trap” click events (or touch
events) on each of them to turn the audio on or off. This is done by starting (or stopping) your
AmbientServiceService subclass, which will then start or stop the MediaPlayer object that is
referencing your ambient planet sound effect digital audio asset. Pretty cool!



  1. Add a line of space underneath your soundOn object instantiation and type
    the soundOn object name again. Then press the period key and select the
    .setOnClickListener( ) method call using the Eclipse pop-up helper dialog.
    You can also simply type the name in.

  2. Inside of the parameter area of the method, type the new Java keyword, and
    then the View object, and the .OnClickListener( ) method, called off of that
    object using dot notation. This can all be done using the following line of
    Java code (shown with comment and indenting), as shown in Figure 13-12:


soundOn.setOnClickListener(new View.OnClickListener() {
// Your event handling program logic will eventually go in here.
});

Figure 13-11. Declare ImageView objects named soundOn and SoundOff and instantiate them using findViewById( )

Free download pdf