0

I think I may have accidentally switched on some setting I didn't intent to in Eclipse or something. I'm working on an android game and from within the game logic, when certain events happen I send messages to the main activity to display certain things.

I have the following classes:

public class GameMessageHandler extends Handler {

    public static final int DO_SOMETHING = 0;

    private final WeakReference<MyActivity> myActivity;

    public GameMessageHandler(MyActivity myActivity) {

        this.myActivity = new WeakReference<MyActivity>(myActivity);

    }

    @Override
    public void handleMessage(Message msg) {

        if (myActivity != null) {

            MyActivity activity = myActivity.get();

            if (msg.what == DO_SOMETHING) {
                //even if the caller only called this one time, this keeps executing forever
                activity.doSomething();

            }
        }
    }
}

public class GameEventListenerAndroid implements GameEventListener {

    private Handler handler;

    public GameEventListenerAndroid(GameMessageHandler gameMessageHandler){
        this.handler = gameMessageHandler;
    }

    @Override
    public void playerTookSomeAction() {
        //this only gets called one time
        handler.sendEmptyMessage(GameMessageHandler.DO_SOMETHING);
    }

}

I KNOW this logic works because what I'm trying to run right now is from a backup I saved after I saw it work MULTIPLE times, but as I said what is happening right now is for some reason the Looper keeps calling the message handler with the same message a million times per second and never discards the message to move on to something else.

Any help is greatly appreciated!

IWriteApps
  • 973
  • 1
  • 13
  • 30

1 Answers1

0

Check points:

  1. activity.doSomething() will not send message GameMessageHandler.DO_SOMETHING
  2. playerTookSomeAction() is not called repeatedly

Just format it to be BETTER, incase someone feels so boring!

Robin
  • 10,052
  • 6
  • 31
  • 52
  • You got it backwards. activity.doSomething() will be called when listener sends message GameMessageHandler.DO_SOMETHING. Also, playerTookSomeAction only gets called one time, but the looper repeatedly calls the message handler with message DO_SOMTEHING. – IWriteApps Feb 27 '14 at 13:45