Creating a Simple Service

To build a service, you extend the abstract class android.app.Service and put a service-configuration entry in your application's manifest file. Listing 8-7 shows an example.

Listing 8-7. A Simple Android Service Definition import android.app.Service;

public class TestServicel extends Service {

private static final String TAG = "TestServicel";

^Override public void onCreate() { Log.d(TAG, "onCreate"); super.onCreate();

^Override public IBinder onBind(Intent intent) { Log.d(TAG, "onBind"); return null;

// service definition entry: must go in the AndroidManifest.xml file. <service android:name="TestService1"></service>

The service in Listing 8-7 isn't meant for practical use, but it serves our purpose of showing how a service is defined. To create a service, you write a class that extends android.app. Service and implements the onBind() method. You then put a service-definition entry in your AndroidManifest.xml file. That is how you implement a service. The next obvious question, then, is this: how do you call the service? The answer depends on the service's client and requires a bit more discussion on services.

0 0

Post a comment