Listing 93 Starting a Service

Available for download on Wrax.com

// Implicitly start a Service

Intent myIntent = new Intent(MyService.ORDER_PIZZA); myIntent.putExtra("TOPPING", "Margherita"); startService(myIntent);

// Explicitly start a Service startService(new Intent(this, MyService.class));

To use this example you would need to include a my_action constant in the MyService class and use an Intent Filter to register the Service as a provider of MY ACTION.

To stop a Service use stopService, passing an Intent that defines the Service to stop. Listing 9-4 first starts and then stops a Service both explicitly and by using the component name returned from a call to startService.

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment