Just An Application

May 14, 2012

Adventures In Distributed Garbage Collection: Part One – An Android Service Implementation

Filed under: Android, Android Internals, Java, Mobile Java, Services — Tags: , , , , — Simon Lewis @ 9:15 am

Arguably one of the less appreciated benefits of writing applications for Android is the opportunity it affords the programmer to quite
inadvertently end up exploring some of the less obvious consequences of distributed garbage collection.

For example here is the AIDL definition of an Android Service.

    package xper.service.fastnet;

    interface Fastnet
    {
        void doNothingInParticular();
    }

and here is one possible implementation of it.

A class implementing the Service

    package xper.service.fastnet;

    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.util.Log;

    public final class FastnetService 
                       extends 
                           Service 
    {
        @Override
        public void onCreate() 
        {
            super.onCreate();
            binder = new FastnetServiceBinder(this);
        }

        @Override
        public IBinder onBind(Intent intent) 
        {
            Log.d(TAG, "onBind(" + intent + ")");
            return binder;
        }
	
        //
	
        @Override
        public boolean onUnbind(Intent intent) 
        {
            Log.d(TAG, "onUnbind(" + intent + ")");
            return super.onUnbind(intent);
        }

	void doNothingInParticular()
	{
		
	}

	//
	
        private FastnetServiceBinder	binder;
	
        //
	
        private static final String TAG = "FastnetService";
    }

A class implementing a Binder for the Service

    package xper.service.fastnet;

    import android.os.RemoteException;
    import xper.service.fastnet.Fastnet.Stub;

    final class FastnetServiceBinder 
                extends 
                    Stub 
    {
        public void doNothingInParticular() 
                    throws 
                        RemoteException 
        {
            service.doNothingInParticular();
        }

        //
	
        FastnetServiceBinder(FastnetService theService)
        {
            service = theService;
        }
	
        //
	
        private FastnetService	service;
    }

This implementation is based on one which appears in the Android documentation except that the Binder class is defined standalone rather than as an inner class to make it slightly more obvious what is going on.

To understand what this has to do with distributed garbage collection it is necessary to know far more about the implementation of Services in Android than you might actually care to.


Copyright (c) 2012 By Simon Lewis. All Rights Reserved.

Advertisements

3 Comments »

  1. […] this example there are three different processes and seven distinct actors of five different types which […]

    Pingback by Adventures In Distributed Garbage Collection: Part Three – The Cast Of Characters: Actors And Processes « Just An Application — May 15, 2012 @ 9:36 am

  2. […] graph was generated using the FastnetService object [0x40517518] as the […]

    Pingback by Adventures In Distributed Garbage Collection: Part Ten – Post Bind Service State « Just An Application — May 21, 2012 @ 6:00 am

  3. […] the previous example the following Android Service implementation creates the Java Binder object returned from the […]

    Pingback by Adventures In Distributed Garbage Collection: Part Seventeen – Binders And Garbage Collection « Just An Application — June 1, 2012 @ 9:33 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: