JavocSoft Android Toolbox – Inter-Application Messaging – Update (10-07-2015)

Hi,

In this update I added some assistance classes related with inter-application messaging in Android to JavocSoft Android Toolbox.JavocSoft Android Toolbox Library

Sometimes an application needs to accept messages from other applications, in those cases, a Messenger service must be implemented. See Messenger for more information about messaging. So, to make easier having a messenger service in an application, i have added a base MessengerService.java class to the library. Click here to access to the Messenger Service Wiki.

 

Here can be seen full example of how to have a valid Messenger Service in your application:

First, create the message incoming handler:

public class MyMessengerIncomingHandler extends MessengerIncomingHandler {

    //Messenger WHAT possible values
    public static final int MSG_EVT_HI = 1000;

    public MyMessengerIncomingHandler(Context context) {
        super(context);
    }

    @Override
    protected void doWork(Message msg) {
        switch (msg.what) {
            case MSG_EVT_HI:
                Log.i(Constants.TAG, "Messenger received: Hi!!");
                break;
            default:
                super.handleMessage(msg);
        }
    }
}

Second, the Messenger Service:

 public class MyMessengerService extends MessengerService {

 @Override
 protected MessengerIncomingHandler getMessageIncomingHandler() {
    return new MyMessengerIncomingHandler(context);
 }

}

And finally, in our AndroidManifest.xml:

<!-- Application messenger -->
<service android:name="es.javocsoft.base.genapp.service.MyMessengerService" 
	 android:enabled="true" android:exported="true" >
   <intent-filter>             
	<action android:name="es.javocsoft.base.genapp.messenger.ACTION_BIND" />
   </intent-filter>
</service>

Because we could need also to connect and send messages to other applications messenger services, I also added a class Mezzenger.java. Click here to access to the Messenger Wiki.

 

Mezzenger messenger = new Mezzenger("Inner", getApplicationContext()); 
messenger.connect("application.package.service.ACTION"); 
messenger.sendMessage(MSG_EVT_HI, 0, 0, null);

 

Do not forget to check the blog to get more updates!

More info and HowTo at https://github.com/javocsoft/javocsoft-toolbox/wiki.

github_icon

As always, library is available on GitHub

javocsoft-toolbox.

Bye.

JavocSoft 2015.

Leave a Reply

Your email address will not be published. Required fields are marked *