Java – Developing a Java application that can run both on the web and as an Android application?

Developing a Java application that can run both on the web and as an Android application?… here is a solution to the problem.

Developing a Java application that can run both on the web and as an Android application?

I’m working on a game that runs both as a mini-program on the web and as an app on an Android phone.

Is it possible to do this, and if so, what do I need to pay attention to to make it work (i.e. if there are any settings I shouldn’t hardcode but determine them based on the user’s device, when the game is running, or any Java libraries I shouldn’t use?). )。

In addition, the game needs to accept a touch screen as input to the Android application. Is it possible to build it into the same game that will also run as a Mini Program? Could it be that at runtime, the applet decides whether to use the mouse or the touch screen for input at runtime?

Solution

While Android applications are written in Java, the framework around the application is very different from the framework around the applet. You will not be able to have an .jar file, you can include it as a small program and throw it on your Android device because it doesn’t work that way.

However, you may be able to create all game logic and objects and share them with applet code and Android applications. You can even put them in a repository and project (although it may have to be an Android project, which you then embed into your app build script).

To handle the different controls of your game, you may have to abstract the input and have your game/level object call back like userHasPoked(int x, int y), and then have the applet call that method when the mouse clicks, and the Android application calls it when it touches (oddly still called onClick).

I think it’s going to be a long road, but it’s a lot easier than rewriting the whole thing. It may seem like more work may be needed earlier, but once you’re done embedding the code into applets and Android apps, you may “never” have to touch that code again and can continue to add to your game.

I wouldn’t underestimate this task, but it sounds like a really fun programming exercise. Good luck!

Related Problems and Solutions