A (175)

(Tuis.) #1

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


This error message tells you that the Context object that you are passing into the Intent( ) constructor
method using the Java this keyword actually contains the Context for the View.OnClickListener
structure, and not for the PlayVideo structure. The PlayVideo Context object is the Context
(application component) that you want to be sending inside of the Intent object that you are
constructing here when it gets delivered (sent over) to the AmbientService Service subclass. So the
question becomes, how do you designate the PlayVideo Context object?


The way to specify the correct Context object, using the Java this keyword, is to preface it with the
path to the PlayVideo class using dot notation. Therefore, this would become PlayVideo.this to
solve this programming quagmire.



  1. The correct startService( ) method call would look like the following Java
    code, as shown in Figure 13-16:


startService( new Intent( PlayVideo.this, AmbientService.class ) );

Figure 13-15. This error message shows that this Context object is the one for the View.OnClickListener( ) code structure

Free download pdf