A (175)

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


  1. Now that you have an error-free soundOn.setOnClickListener( ) event
    handling structure that contains the correct startService( ) method call,
    you can copy and paste the entire structure to create your soundOff event
    handling structure. That structure will call the stopService( ) method call to
    stop the background audio playback.


The Java code blocks for both event-handling structures will look like the following, as shown in
Figure 13-17:


soundOn = (ImageView)findViewById(R.id.playAudio);
soundOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
startService( new Intent( PlayVideo.this, AmbientService.class) );
}
});
soundOff = (ImageView)findViewById(R.id.stopAudio);
soundOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
stopService( new Intent( PlayVideo.this, AmbientService.class) );
}
});

Figure 13-16. Add a PlayVideo.this path reference to the this keyword using dot notation to fix the error

Free download pdf