IPC EventBus

General

Category
Free
Tag
Event Buses
License
N/A
Min SDK
8 (Android 2.2–2.2.3 Froyo)
Registered
May 7, 2017
Favorites
2
Link
https://github.com/NewtronLabs/IpcEventbus
See also
RxBus
AndroidKVO
EventBus Demo
fast-event
RxBus

Additional

Language
Java
Version
N/A
Created
Mar 14, 2017
Updated
Jul 23, 2018
Owner
Newtron Labs (NewtronLabs)
Contributor
Newtron Labs (NewtronLabs)
1
Activity
Badge
Generate
Download
Source code
APK file

Commercial

IPC EventBus

Faster than Intents and easier than AIDLs. IPC EventBus is an Android library for sending events between processes or different apps.


In the build.gradle for your app include:

dependencies {
    compileOnly 'com.newtronlabs.ipceventbus:ipceventbus:5.1.0'
}

Step 2

Implement IIpcEventBusConnectionListener and IIpcEventBusObserver.

public class Listener implements IIpcEventBusConnectionListener, IIpcEventBusObserver 
{
    public Listener() 
    {
        String targetApp = "com.packagename";
        
        IIpcEventBusConnector connector =
                ConnectorFactory.getInstance().buildConnector(context, this, targetApp);
            
        connector.startConnection();
    }

    @Override
    public void onConnected(IIpcEventBusConnector connector) 
    {
        connector.registerObserver(this);
    }

    @Override
    public void onEvent(IEventIpc event) 
    {
        Log.d("ipceventbus", "Received event: " + event.getClass().getSimpleName());
    }

    @Override
    public void onDisconnected(IIpcEventBusConnector connector) 
    {

    }
}

Step 3

IpcEventBus.getInstance().postEvent(new MyEvent());

Step 4

Create a module with all your events. Then share the module between the apps that will share the events. Ideally you would turn this module into an AAR for easy sharing.

Option A - Simple

The easiest way to create an event is to make your event extend EventIpcSimple so that all the setup happens in that super class.

public class EventExample extends EventIpcSimple
{
    public EventExample()
    {
    }

    EventExample(Parcel in)
    {
        readFromParcel(in);
    }
}

Option B - Event containing object(s)

Sometimes it you may which to pass data inside of an event for it to be received on another app. To do this you have to use the ParcelHelper class that will do all the heavy lifting for you. The example below shows this. It also shows that interface types are preserved accross IPC.

public class EventExample extends EventIpcSimple 
{
    private IData mData;

    public EventExample(IData data)
    {
        mData = data;
    }

    EventExample(Parcel in)
    {
        readFromParcel(in);
    }

    @Override
    public void readFromParcel(Parcel parcel)
    {
        // Must be done for every object in the event.
        mData = (IData) ParcelHelper.getInstance().createFromParcel(parcel, IData.class);
    }

    @Override
    public void writeToParcel(Parcel dest, int flags)
    {
        // Must be done for every object in the event.
        ParcelHelper.getInstance().writeToParcel(dest, flags, mData);
    }

    public IData getData()
    {
        return mData;
    }
}

Option C - Advance mode

Sometimes you don't want your event to extend EventIpcSimple.

public class EventExample implements IEventIpc
{
    public EventExample()
    {
    }

    EventExample(Parcel in)
    {
        readFromParcel(in);
    }
    
    public static final Creator<EventExample> CREATOR = new Creator<EventExample>()
    {
        @Override
        public EventExample createFromParcel(Parcel in)
        {
            return new EventExample(in);
        }

        @Override
        public EventExample[] newArray(int size)
        {
            return new EventExample[size];
        }
    };
    
     @Override
    public int describeContents()
    {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags)
    {        
    }

    public void readFromParcel(Parcel in)
    {
    }
}

Additional Samples

A set of more complex exmaples can be found in this repo's samples folders: App 1 and App 2.