Feed on


In the following are reported the guidelines for realise step by step the implementation of the TestSmsViewer example. If you do not want to code by hand you can find the Eclipse project related to this example inside the utils-projects folder of the standard JaCa-Android distribution.


Modify the Application main activty as showed above. This activity must extends the JaCaActivity class (a JaCa-Android extension of the standard Android activity class).

package jaca.android.example;

import jaca.android.dev.JaCaActivity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SmsViewer extends JaCaActivity {

    private Handler mHandler;
    private ArrayAdapter mConversationArrayAdapter;
    private ListView mConversationView;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
	    mConversationArrayAdapter = new ArrayAdapter(this, R.layout.message);
        mConversationView = (ListView) findViewById(R.id.in);
        mHandler = new Handler();
	    createArtifact("viewer", ViewerArtifact.class.getCanonicalName());

	public void append(final String source, final String msg){
		mHandler.post(new Runnable() {
			public void run() {
				mConversationArrayAdapter.add(source+":  " + msg);

What it is worth of particular attention here is:

  • The runJaCaService method: we use this method to run our JaCa MAS. This method need the string resources that identify the mas2j file path.
  • The append method: we use this method for adding a new SMS to the GUI. Note this method is always called by the ViewerArtifact wrapping the activity (see bellow).

XML files used for realising the GUI

We report here the xml file used fore defining the GUI of the sample application.

The main GUI: sms_viewer.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    <ListView android:id="@+id/in"

The GUI used for displaying each message on screen: message.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"


We will use this artifact for managing the application GUI (i.e. if you take a look at the init of the artifact you will see that the ViewerArtifact wrap the real application GUI, the SmsViewer JaCaActivity). What is worth of particular attention in this class is the append operation. This operation allows us to add a new message to the list of the received messages of the wrapped SmsViewer. For more details about realising GUI in JaCa-Android see here.

package jaca.android.example;

import jaca.android.dev.GUIArtifact;
import jaca.android.dev.IJaCaActivity;
import android.os.Bundle;
import cartago.INTERNAL_OPERATION;
import cartago.OPERATION;
import cartago.ObsProperty;

public class ViewerArtifact extends GUIArtifact{

	private SmsViewer mViewer;

	protected void init(IJaCaActivity activity, Bundle savedInstanceState) {
		super.init(activity, savedInstanceState);
		mViewer = (SmsViewer) activity;
		defineObsProperty("viewer_state", "not_displayed");

	@OPERATION public void addSMSToList(String source, String msg){
    	mViewer.append(source, msg);

    @INTERNAL_OPERATION void onStart(){
    	ObsProperty obsProp = getObsProperty("viewer_state");

		ObsProperty obsProp = getObsProperty("viewer_state");


Add a new agent called agent creating a file agent.asl with the following content in the jaca.android.example package. This is the agent we were refering in the example.mas2j file. The agent source code is quite simple. We skip for now deep explanations of the init plan, will be deeply discussed in the JaCa-Android by example tutorial, for now is sufficient to know that this plan takes care of the join to the MiddlewareServices shared workspace. Once the agent has joined this workspace it can start its job (plan do_job). The agent focus the sms-manager artifact for waiting events related to SMS reception. As soon an event indicating a new SMS is perceived (+sms_received(Source, Message)) the addSMSToList operation of the ViewerArtifact operation is called for appending the new message to the list.


  <-  !init;

 * Initialisation plan for joining the MiddlewareServices workspace
  <-  lookupArtifact("console", LocalConsoleID);
       lookupArtifact("viewer", ViewerID);
       +art_id(viewer, ViewerID);

+!try_join : art_id(console,LocalConsoleID) & join_attempts(N) & N < 3
  <-  -+join_attempts(N+1);
       joinRemoteWorkspace("MiddlewareServices", "jaca.android.jacaservice", WspID);
       println("Join to MiddlewareServices wsp done.")[artifact_id(LocalConsoleID)].

-!try_join : join_attempts(N) & N >= 3
  <-  println("Failure, cannot join MiddlewareServices wsp.")[artifact_id(LocalConsoleID)].

  <-  println("Failure,
           trying join MiddlewareServices again in one second.")[artifact_id(LocalConsoleID)];

/******************** Main agent plan ********************/		

+!do_job: true
  <-  focusWhenAvailable("sms-manager");
       .println("init done").

+sms_received(A, B) : art_id(viewer, ViewerID)
  <-  addSMSToList(A, B)[artifact_id(ViewerID)].

Below is reported a screenshot of the running application.

Comments are closed.