Author: Carl Nagle Since: FEB 24, 2012 Updated: FEB 27, 2012 Carl Nagle, org.safs.android.messenger.client info Copyright SAS Institute, Inc. http://www.sas.com General Public License (GPL) http://www.opensource.org/licenses/gpl-license.php
The SAFS TCP Messenger Service for Android is an Android application that provides general-purpose TCP messaging services between remote clients and installed Android applications that DO NOT have Android INTERNET permissions.

For example, Android test applications testing Android application that do not have INTERNET permissions are themselves not allowed to have INTERNET permissions. By using the SAFS TCP Messenger Service the test application can communicate with a remote test controller, receive external commands via TCP sockets, and return test results or data to the remote controller.
The initial uses for this SAFS TCP Messenger Service are to provide for Robotium Remote Control and SAFS Android Remote Control using the SAFS Automation Framework. However, the general-purpose nature of the SAFS TCP Messenger Service makes it suitable for other uses on Android independent of these test automation solutions.
Upon release, an independent SAFS TCP Messenger will be available from the SAFS Downloads Page.
The SAFS TCP Messenger will also be bundled as part of a future Robotium Remote Control download, and/or a future SAFS Android Support download.
Install the SAFS TCP Messenger application from the Android Market(future), or from your local Android development environment or network repository. A typical command-line install using the Android SDK tools would be:
adb install <pathTo>\SAFSTCPMessenger-debug.apk
(Normally these two are coordinated efforts since both sides have to interpret the messaging.)
The JAR file must be in the Android project's /libs directory for the Ant build to see, and/or otherwise referenced as a developer's Java Build Path dependency for remote TCP client development.
The JAR file must be in the Android project's /libs directory for the Ant build to see.
Generally, developers wishing to create a remote TCP client simply have to implement a CommandListener interface listening to an instance of a MessengerRunner.
Developers provide their own message content and syntax to be transported over the predefined SocketProtocol. This content will ultimately be received by the SAFS TCP Messenger and forwarded to the Android application. So the Android application must be coded to understand the content, know how to parse it, and know what the remote client might be expecting in return.
The SAFS TCP Messenger doesn't care about the message syntax as long as the content conforms to being transported over the SocketProtocol.
The Android application must will have the following Java library dependencies previously mentioned. These will reside in the Android project's /libs directory:
The Android application will generally launch the SAFS TCP Messenger during it's onCreate method to insure the Service is available when the application is launched. The application will bind with the Service--usually with an instance of a MessengerRunner as shown below.
import org.safs.android.messenger.MessageUtil;
import org.safs.android.messenger.client.CommandListener;
import org.safs.android.messenger.client.MessengerRunner;
import org.safs.android.messenger.client.MessageResult;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
public class YourAppClass extends <YourAppSuperclass> implements CommandListener{
public void onCreate(Bundle savedInstanceState){
if(!doBindService()){
return;
}
start();
}
ServiceConnection mConnection = new ServiceConnection(){
public void onServiceConnected(ComponentName className, IBinder service){
Messenger mService = new Messenger(service);
if(messageRunner==null){
messageRunner = new MessengerRunner(mService, this);
messageRunner.start();
messageRunner.sendRegisterEngine();
}
}
public void onServiceDisconnected(ComponentName className){
if(messageRunner != null){
messageRunner.sendUnRegisterEngine();
messageRunner = null;
}
}
};
boolean mIsBound = false;
protected boolean doBindService(){
try{
mIsBound = getContext().bindService(new Intent(MessageUtil.SERVICE_CONNECT_INTENT), mConnection, Context.BIND_AUTO_CREATE);
return mIsBound;
}catch(Exception x){
return false;
}
}
}
// implement your CommandListener interface below
Then, if your CommandListener needs to respond to any incoming message it can do so with the methods already available from the MessengerRunner:
messageRunner.sendReady();
messageRunner.sendRunning();
messageRunner.sendServiceResult(java.util.Properties);
messageRunner.sendServiceResult(int,java.lang.String);
messageRunner.sendMessage(java.lang.String);
messageRunner.sendException(java.lang.String);
messageRunner.sendDebug(java.lang.String);
messageRunner.sendShutdown();
The Android SDK provides examples of implementing Android Messaging with Android services. Our implementations for both Robotium Remote Control and SAFS Remote Control also serve as real-world examples of using the SAFS TCP Messenger Service specifically.
The Android application will ultimately be communicating with the SAFS TCP Messenger using the Inter-Process Communication (IPC) protocols provided by the Android OS.