Just An Application

May 14, 2012

Adventures In Distributed Garbage Collection: Part Two – Setting The Scene: Interfaces And Base Classes

1.0 IPC Related Interfaces And Base Classes

1.1 Interfaces

1.1.1 IBinder

Package: android.os

Source: frameworks/base/core/java/android/os/IBinder.java

The IBinder interface defines the set of methods that provide access to the Android remote procedure call mechanism from Java.

1.2 Classes

1.2.1 Binder

Package: android.os

Source: frameworks/base/core/java/android/os/Binder.java

Definition: 44-336

The Binder class implements the IBinder interface.

It provides access to the concrete implementation of the Android remote procedure call mechanism from Java.

1.2.2 Binder Proxy

Package: android.os

Source: frameworks/base/core/java/android/os/Binder.java

Definition: 338-395

The BinderProxy class implements the IBinder interface.

An instance of the Binder class or an instance of a sub-class of the Binder class can be written to a Parcel using the method

    public final native void writeStrongBinder(IBinder val);

and the Parcel sent to another process.

The recipient can use the Parcel method

    public final native IBinder readStrongBinder();

to read the instance from the Parcel.

What the method actually returns is a BinderProxy object which can be used to invoke any of the methods in IBinder interface on the Binder object in the originating process.

A BinderProxy object can also be written to a Parcel in exactly the same way as a Binder object.

If the Parcel is sent to the process in which the original Binder object is located then when the recipient calls Parcel.readStrongBinder() the Binder object itself is returned. If the Parcel is sent to any other process when the recipient calls Parcel.readStrongBinder() it returns an equivalent BinderProxy object.

2.0 Application Related Interfaces And Base Classes

2.1 Interfaces

1.1 IActivityManager

Package: android.app

Source: frameworks/base/core/java/android/app/IActivityManager.java

The IActivityManager interface is an internal cross-process interface used by an Android application to interact with the Android activity manager.

The interface defines a number of methods, of which the following are Service related.

    ...

    public int bindService(
                   IApplicationThread caller, 
                   IBinder token,
                   Intent service, 
                   String resolvedType,
                   IServiceConnection connection, 
                   int flags) 
               throws 
                   RemoteException;

    public boolean unbindService(IServiceConnection connection) 
                   throws 
                       RemoteException;

    public void publishService(IBinder token, Intent intent, IBinder service) 
                throws 
                    RemoteException;

    public void unbindFinished(IBinder token, Intent service, boolean doRebind) 
                throws 
                    RemoteException;

    public void serviceDoneExecuting(IBinder token, int type, int startId, int res) 
                throws 
                    RemoteException;

    public IBinder peekService(Intent service, String resolvedType) 
                   throws 
                       RemoteException;
					   
    ...

1.2 IApplicationThread

Package: android.app

Source: frameworks/base/core/java/android/app/IApplicationThread.java

The IApplicationThread interface defines the set of internal cross-process methods used by the Android activity manager to manage an Android application.

The interface defines a number of methods of which the following are Service related.

    ...

    public void scheduleCreateService(IBinder token, ServiceInfo info) 
                throws 
                    RemoteException;

    public void scheduleBindService(IBinder token, Intent intent, boolean rebind) 
                throws 
                    RemoteException;

    public void scheduleUnbindService(IBinder token, Intent intent) 
                throws 
                    RemoteException;

    public void scheduleServiceArgs(IBinder token, int startId, int flags, Intent args)
                throws 
                    RemoteException;
    
    public void scheduleStopService(IBinder token) 
                throws 
                    RemoteException;

    ...

3.0 Service Related Interfaces And Base Classes

3.1 Interfaces

3.1.1 IServiceConnection

Package: android.app

Source: frameworks/base/core/java/android/app/IServiceConnection.aidl

The IServiceConnection interface defines the internal cross-process interface used by the Android activity manager to interact with Service clients.

3.1.2 ServiceConnection

Package: android.content

Source: frameworks/base/core/java/android/content/ServiceConnection.java

The ServiceConnection interface is a public interface which defines two methods.

    public void onServiceConnected(ComponentName name, IBinder service);

    public void onServiceDisconnected(ComponentName name);

A client connecting to an Android Service must pass an instance of a class which implements this interface when it calls the bindService() method.

The onServiceConnected() method of the object will be invoked when a connection to the Service is established.

The name argument identifies the Service Component to which the connection has been established, and the service argument is an object which can be used to interact with the Service component.

The onServiceDisconnected() method is invoked when the Service is no longer available.

The name argument identifies the Service Component that has been disconnected from.

3.2 Base Classes

3.2.1 Service

Package: android.app

Source: frameworks/base/core/java/android/app/Service.java

The Service class is the abstract base class from which all Android Service Component classes must inherit.

It defines a single abstract method

    public abstract IBinder onBind(Intent intent);

The method implementation should return an object which implements the IBinder interface.

A successful call to this method establishes a binding between the Intent specified and the IBinder object returned.

The method is invoked each time a connection is made to the Service using an Intent which is not filterEquals() to any Intent for which an Intent/IBinder binding has already been established.

A proxy for the IBinder object bound to the specified Intent will be passed back to the client which is connectiong to the Service via a call to the implementation of the ServiceConnection method onServiceConnected().


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

Advertisements

11 Comments »

  1. […] ActivityThread object has an associated ApplicationThread object which implements the IApplicationThread […]

    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. […] ActivityManagerProxy class implements the IActivityManager […]

    Pingback by Adventures In Distributed Garbage Collection: Part Five – The Cast Of Characters Concluded: Proxies « Just An Application — May 16, 2012 @ 8:25 am

  3. […] is defined by the IActivityManager interface, or via an internal call in the ActivityManagerService itself to the […]

    Pingback by Adventures In Distributed Garbage Collection: Part Six – Service Creation « Just An Application — May 17, 2012 @ 12:13 am

  4. […] is stopped via a remote procedure call to either the ActivityManagerService implementation of the IActivityManager […]

    Pingback by Adventures In Distributed Garbage Collection: Part Seven – Service Destruction « Just An Application — May 17, 2012 @ 8:50 am

  5. […] ConnectionRecord in the connections map of the ServiceRecord the method calls connected() on the IServiceConnection object held by the ConnectionRecord (conn) passing null as the second argument to indicate that the […]

    Pingback by Adventures In Distributed Garbage Collection: Part Seven – Service Destruction « Just An Application — May 17, 2012 @ 9:00 am

  6. […] the ServiceConnection object that was originally passed to the bindService() […]

    Pingback by Adventures In Distributed Garbage Collection: Part Twelve – Unbinding From A Service « Just An Application — May 23, 2012 @ 6:00 am

  7. […] the ServiceRecord class ? It does not have any native methods, on the other hand its super class android.os.Binder does so what about them […]

    Pingback by Adventures In Distributed Garbage Collection: Part Sixteen – The Great ServiceRecord Mystery « Just An Application — May 30, 2012 @ 9:10 am

  8. […] finalize() method of a Java BinderProxy object, that is an instance of the class android.os.BinderProxy, calls the native method destroy() which decrements the reference count of the associated Binder […]

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

  9. […] reference to the ServiceRecord object [0x406f89b8] which means that there exists at least one BinderProxy somewhere that references […]

    Pingback by Adventures In Distributed Garbage Collection: Part Eighteen – The Great Service Record Mystery Revisited « Just An Application — June 6, 2012 @ 9:32 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

Create a free website or blog at WordPress.com.

%d bloggers like this: