Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android message between classes

Tags:

java

android

This is my first post and I did not find anything similar, so I've decided to ask.

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.

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 :)

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.

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.

Below are the snippet from my activity:

package my.poker.game;

//import stuff

public class ActivityHand extends Activity
{
private static Vector<Player> Players = new Vector<Player>();
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);
    }
}

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

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

Thanks a lot for your help


Editing: I've decided to try implementing it using a Handler and passing simple messages. As I read from Handler at Developers a Handler object is assigned to thread's MessageQueue, so I tried to put this into my Activity Class (before Hand hand = new Hand (...) ):

Handler handler = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {

        Bundle bundle = msg.getData();

        // Do something with message contents
    }
};

And inside the Hand class I put this when I want to draw something:

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);

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.

What am I missing?

Thanks again

like image 532
William Miranda Avatar asked Oct 17 '12 14:10

William Miranda


People also ask

What are developer messages?

Developer's message publishes notices about exceptions that affect user experience to respond to users promptly. The content is displayed under Comments on the game details page.

What is Handler in Android example?

A Handler is a threading class defined in the android. os package through which we can send and process Message and Runnable objects associated with a thread's MessageQueue . You start by creating a Handler instance. Then that instance gets associated with a single thread as well as that thread's message queue.


1 Answers

To call methods in the activity, you want to pass the activity to this class.

for example:

    public class PokerActivity extends Activity
        {

           @Override
           public void onCreate(Bundle savedInstanceState)
           {
                Hand hand = new Hand(this);
           }

           public void setVisibleHand(Player player)
           {
            <do something in the activity>
           }

        }

    public class Hand
    {
      PokerActivity pokerActivity;
      public Hand(PokerActivity activity)
      {
        this.pokerActivity = activity;
      }

      public void setVisibleHand()
      {
        pokerActivity.setVisibleHand(player1);
      }
    }

Now, this might not be the best way to do it. In Android you have to be carefull to not leak the context, or you might be getting trouble with the memory. (simply passing the activity/context might be the easy way, but is also the easiest way to leak the context.)

I'd advise you to look at some simple tutorials first, to get a feeling of how android activities work.

like image 88
NickL Avatar answered Sep 29 '22 05:09

NickL