Android Application Event Handling

Is there a standard set of Listener / Observer / Observable classes in Android for managing application events in Android?

I'm not talking about user interfaces or other Android API events, but rather about application user events like LevelClearedEvent , LevelClearedEvent , etc.

Is there a preferred implementation / extension interface so that I can implement things like:

 public void addGameOverListener(GameOverListener listener) 
+4
source share
2 answers

It's simple, you just need to create your own EventListener

 public interface onGameFinishedListener { public void onGameFinished(GameView gameView); } 

and some class that has the onGameFinished () method

 public abstract class GameView extends SurfaceView implements SurfaceHolder.Callback{ List<onGameFinishedListener> listeners; public GameThread gameThread; protected int width; protected int height; public GameView(Context context) { super(context); width = 320; height = 480; listeners = new ArrayList<onGameFinishedListener>(); } public abstract void init(); public void registerGameFinishedListener(onGameFinishedListener listener) { listeners.add(listener); } protected void GameFinished(GameView gameView) { for (onGameFinishedListener listener : listeners) { synchronized(gameThread.getSurfaceHolder()) { listener.onGameFinished(gameView); } } } } 

and then you implement onGameFinishedListener in your activity or view that you want to execute when you finish the game,

 public class RocketActivity extends GameActivity implements onGameFinishedListener { private final int MENU = 0; private final int END = 1; private final int CONFIRMATION = 2; private RelativeLayout layout; private RocketView rocketView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); layout = new RelativeLayout(this); rocketView = new RocketView(this); rocketView.registerGameFinishedListener(this); rocketView.init(); layout.addView(rocketView); setContentView(layout); } @Override public void onGameFinished(GameView gameView) { runOnUiThread(new Runnable() { @Override public void run() { showDialog(END); } }); } 

}

there. no need to rely on Android for EventListener. :)

+3
source

Have you tried the GreenRobot EventBus?

This is basically a fairly standard implementation of eventBus for handling large-scale application events. It provides a thread-to-thread relationship, which is pretty neat.

Pretty similar to what you get for GWT

+1
source

Source: https://habr.com/ru/post/1345256/


All Articles