Implementing an AIDL Interface

In the previous section, we defined an AIDL file for a stock-quoter service and generated the binding file. Now we are going to provide an implementation of that service. To implement the service's interface, we need to write a class that extends android.app.Service and implements the IStockOuoteService interface. To expose the service to clients, we need to provide an implementation of the onBind() method and add some configuration information to the AndroidManifest.xml file. Listing 8-12 shows an implementation of the IStockOuoteService interface.

Listing 8-12. The IStockQuoteService Service Implementation // StockOuoteService.java package com.syh;

import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException;

public class StockOuoteService extends Service {

private NotificationManager notificationMgr;

public class StockOuoteServiceImpl extends IStockOuoteService.Stub {

^Override public double getOuote(String ticker) throws RemoteException {

return 20.0;

^Override public void onCreate() { super.onCreate();

notificationMgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

displayNotificationMessage("onCreate() called in StockOuoteService");

^Override public void onDestroy() {

displayNotificationMessage("onDestroy() called in StockOuoteService"); super.onDestroy();

^Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId);

^Override public IBinder onBind(Intent intent) {

displayNotificationMessage("onBind() called in StockOuoteService"); return new Stock0uoteServiceImpl();

private void displayNotificationMessage(String message) {

Notification notification = new Notification(R.drawable.note, message,System.currentTimeMillis());

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,new Intent(this, MainActivity.class), 0);

notification.setLatestEventInfo(this, "Stock0uoteService",message, contentIntent);

notificationMgr.notify(R.string.app_notification_id, notification);

The StockOuoteService.java class in Listing 8-12 resembles the local BackgroundService we created earlier. The primary difference is that we now implement the onBind() method. Recall that the Stub class generated from the AIDL file was an abstract class and that it implemented the IStockOuoteService interface. In our implementation of the service, we have an inner class that extends the Stub class called StockOuoteServiceImpl. This class serves as the remote-service implementation, and an instance of this class is returned from the onBind() method. With that, we have a functional AIDL service, although external clients cannot connect to it yet.

To expose the service to clients, we need to add a service declaration in the AndroidManifest. xml file, and this time, we need an intent-filter to expose the service. Listing 8-13 shows the service declaration for the StockOuoteService.

Listing 8-13. Manifest Declaration for the IStockQuoteService <service android:name="StockOuoteService">

<intent-filter>

<action android:name="com.syh.IStockOuoteService" /> </intent-filter>

As with all services, we define the service we want to expose with a <service> tag. For an AIDL service, we also need to add an <intent-filter> with an <action> entry for the service interface we want to expose.

With this in place, we have everything we need to deploy the service. Let's now look at how we would call the service from another application (on the same device, of course).

0 0

Post a comment