Search code examples
javaandroidinitializationandroid-asynctaskgame-engine

How to implement initialization with AsyncTask


I would like to initialize all the components of the game engine with the AsyncTask. Could somebody guide me how this would be done?

I'd like something like this: 1. Before the code runs, set a splash screen (.xml) 2. run the initialization code 3. once all done, run the load screen of the game

Here's my current code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Display splash screen
        if(this.splashScreen != null) {
            // .xml
            setContentView(this.splashScreen);
        }

        // Do all the initialization

        // Acquire a wakeLock to prevent the phone from sleeping
        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLGame");

        // Setup all the Game Engine components 
        gameEngineLog = new WSLog("WSGameEngine");
        gameEngineLog.setLogType(this.gameEngineLogType);
        gameLog = new WSLog(this.gameLogTAG);
        gameLog.setLogType(this.gameLogType);
        io = new FileIO(this, getAssets());
        audio = new Audio(this);
        wsScreen = new WSScreen(this, this.screenResizeType, this.customTopYGap, this.customLeftXGap, this.gameScreenWidth, this.gameScreenHeight);
        graphics = new Graphics(this, wsScreen.getGameScreen(), wsScreen.getGameScreenextended());
        renderView = new RenderView(this, wsScreen.getGameScreen(), wsScreen.getGameScreenextended(), FPS, maxFrameskippes);
        input = new Input(this, renderView, logGameEngineInputLog);
        networkSocket = new NetworkSocket(this);

        this.gameEngineLog.d(classTAG, "Completed initialization");
        setContentView(renderView);

        // Check that the developer has initialized the assets
        if(this.assets == null) {
            this.gameEngineLog.w(classTAG, "The assets for the game haven't been defined!");
        }

        this.getNetworkSocket().useAnalytics(this.analyticsAppAPIKey);
        this.getNetworkSocket().useServerCommunication(this.serverCommunicationAppID, this.serverCommunicationClientKey);
        this.getNetworkSocket().useFacebookCommunicaiton(this.facebookCommunicationAppID);

        // Check if server communication should be initialized
        if(this.networkSocket.getUseOfServerCommunication() == true) {
            this.networkSocket.getServerCommunication().initialize(this, this.networkSocket.getServerCommunicationAppID(), this.networkSocket.getServerCommunicationClientKey()); 
        }

        // Check if facebook communication should be initialized
        if(this.networkSocket.getUseFacebookCommunication() == true) {
            this.networkSocket.getFacebookCommunication().initialize(this.networkSocket.getFacebookCommunicationAppID(), true);
        }

        // Start the Load screen
        // Once all of this code has been executed, the class that extends this class calls "setScreen(new LoadScreen(this));" to set the LoadScreen, which
        // loads all the assets of the game

    }

Solution

  • According to the asynctask documentation the class exposes four methods that you can use to implement your application loading logic:

    onPreExecute(), invoked on the UI thread immediately after the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.

    doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time.

    onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). This method is used to display any form of progress in the user interface while the background computation is still executing.

    onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

    Following the documentation, you can start your async task in your main Activity. In the onPreExecute method you can display your splash screen.

    In the doInBackGround method insert the code that does all the loading stuff. If you want to provide the user with some information about the loading state you can use inside this method publishProgress to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.

    Finally in onPostExecute method you can remove the splashscreen and run the load screen of the game.

    have a look also at this example.