Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

2. Working with Services > Controlling a Service

Controlling a Service

At this point, the example code has a complete implementation of a Service. Now we write code to control the service we previously defined.

Intent service = new Intent("com.androidbook.GPXService.SERVICE");
service.putExtra("update-rate", 5000);
startService(service);

Starting a service is as straightforward as creating an Intent with the service name and calling the startService() method. In this example, we also set the Intent extra parameter called update-rate to 5 seconds. That rate is quite frequent but works well for testing. For practical use, we probably want this set to 60 seconds or more. This code triggers a call to the onCreate() method, if the Service isn’t bound to or running already. It also triggers a call to the onStart() or onStartCommand() methods, even if the service is already running.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial