Android message between classes
<p>This is my first post and I did not find anything similar, so I've decided to ask.</p>

<p>Im developing a Poker Game for Android to practice the SDK and refresh/improve my Java. Its a simple app that control a texas hold'em poker hand.</p>

<p>Initally, I wrote my classes using only Java SE and it looks fine. Each class has its own purpose and testing it with console input/output, I can see it really works :)</p>

<p>Last week, I decided to port it to Android to see things happening through a graphic interface, so I got the resource images, make an Activity and included my poker package.</p>

<p>Before port to Android I can just put a println (or readLine) to see whats going on and send my inputs. Now Im stuck in how each class can communicate to the game activity to provide what must be drawn. If possible, I don't want insert Android draw code inside game classes. Im trying find a way to exchange messages between my Activity and the game classes and Id like some suggetions. Im new in developing Android apps, so I dont know all mechanics to do this.</p>

<p>Below are the snippet from my activity:</p>

<pre><code>package my.poker.game;

//import stuff

public class ActivityHand extends Activity
{
private static Vector&lt;Player&gt; Players = new Vector&lt;Player&gt;();
public static final int MAX_PLAYERS = 8;

public static void startEmptyTable()
{
    Players.removeAllElements();
    Players.setSize(MAX_PLAYERS);
}

public static void LeaveTable(int pos)
{
    Players.set(pos, null);
}

public static void SitTable(int pos, Player player)
{
    Players.set(pos, player);
}

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        int cash = 1000;

        startEmptyTable();

        SitTable(0, new Jogador("William", cash));
    SitTable(2, new Jogador("Richard", cash));
    SitTable(4, new Jogador("John", cash));
    SitTable(6, new Jogador("Paul", cash));
    SitTable(8, new Jogador("Albert", cash));

    //Start a Hand.... in future this will be a loop for each Hand
        Hand hand = new Hand(Players);
    }
}
</code></pre>

<p>The object hand, will select a Dealer, deal the cards, control the small and big blinds and start the game loop.</p>

<p>The question is: how the hand class can tell the Activity to Draw and pass an object containing what to draw?</p>

<p>Thanks a lot for your help</p>

<hr>

<p>Editing: I've decided to try implementing it using a Handler and passing simple messages. As I read from <a href="http://developer.android.com/reference/android/os/Handler.html" rel="nofollow">Handler at Developers</a> a Handler object is assigned to thread's MessageQueue, so I tried to put this into my Activity Class (before Hand hand = new Hand (...) ):</p>

<pre><code>Handler handler = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {

        Bundle bundle = msg.getData();

        // Do something with message contents
    }
};
</code></pre>

<p>And inside the Hand class I put this when I want to draw something:</p>

<pre><code>Handler handler = new Handler();
Message msg = new Message();
Bundle bundle = new Bundle();

bundle.putString("Key", "The Key's Value");
msg.setData(bundle);
handler.sendMessage(msg);
</code></pre>

<p>As I understood, both handlers are from same thread, so they are assigned to same MessageQueue right? I tought when I send a message inside Hand class, the Activity class can receive it in handleMessage method and processes any message I send, but handleMessage doesn't execute.</p>

<p>What am I missing?</p>

<p>Thanks again</p>
