CL So
CL So

Reputation: 3759

Let the splash activity wait a process

My app have a splash activity

It must display at least 5 seconds

But in this activity I have another thread to sync data from internet

Sync process may take more than 5 seconds or less than 5 second.

If less than 5 seconds, the Handler should wait until fifth second

If more then 5 seconds, the Handler should wait until process complete

How to make the Handler wait another thread?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    syncFromInternet(); // another thread may over 5 seconds

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent;
            intent = new Intent(getApplicationContext(), SecondActivity.class);
            startActivity(intent);
        }
    }, 5000);

}

Upvotes: 1

Views: 786

Answers (5)

Vishal Makasana
Vishal Makasana

Reputation: 960

You have to make just simple logic Like

When handler completes then check for completion of syncFromInternet method, if it completed open main Activity

When syncFromInternet completes then check for completion of handler, if it completed open main Activity.

Above explanation in code:

boolean isHandlerCompleted = false, isAsyncCompleted = false;

new Handler().postDelayed(new Runnable() {

    @Override
    public void run() {

        isHandlerCompleted = true;

        if (isHandlerCompleted && isAsyncCompleted) {
            openMainActivity();// both thread completed 
        }
    }
}, 5000);


// in your async task add this condition when it completes its task
isAsyncCompleted = true;

if (isHandlerCompleted && isAsyncCompleted) {
    openMainActivity();// both thread completed 
} // till this line

// make this function to open main activity
openMainActivity() {
    Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
    startActivity(intent);
}

Upvotes: 1

Pranay Narvankar
Pranay Narvankar

Reputation: 52

Call your handler inside syncFromInternet() after execution of thread.

Upvotes: 0

AndiGeeky
AndiGeeky

Reputation: 11464

You can use this code :

package com.example.untitled;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.File;
import java.io.IOException;

public class MyActivity extends Activity {

    private volatile boolean isAvailable = false;
    private volatile boolean isOver = false;

    private Handler messageHandler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    if (isOver) {
                        Log.e("messageHandler","isOver");
                        transitToNewActivity();
                    }else {
                        Log.e("messageHandler","isOver false");
                    }
                    break;
            }
        }
    };

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        syncFromInternet(); // another thread may over 5 seconds

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                isOver = true;
                Log.e("mainHandler", "Main handler expired");
                if (isAvailable) {
                    transitToNewActivity();
                    Log.e("mainHandler", "isAvailable");
                }else {
                    Log.e("mainHandler","isAvailable false");
                }
            }
        }, 50000);

    public void transitToNewActivity() {
        Log.e("transitToNewActivity","Activity transited");
        Intent intent;
        intent = new Intent(getApplicationContext(), SecondActivity.class);
        startActivity(intent);
    }

    public void syncFromInternet() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(80000);
                    isAvailable = true;
                    messageHandler.sendEmptyMessage(0);
                    Log.e("syncFromInternet", "internet data synced");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }
}

and please modify it according to your requirement.

Upvotes: 1

Kiril Aleksandrov
Kiril Aleksandrov

Reputation: 2591

You can use an AsyncTask instance and measure the time for syncing with the remote server. If the time is greater than 5 minutes, start the new activity, otherwise - wait till the 5th second:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {

        private static final long FIVE_SECONDS = 5 * 1000;
        private volatile Date mStartTime;

        @Override
        protected void onPreExecute() {
            mStartTime = new Date();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Do the syncing here
            syncFromInternet();

            Date now = new Date();
            long execTime = now.getTime() - mStartTime.getTime();
            if(execTime < FIVE_SECONDS) {
                Thread.sleep(FIVE_SECONDS - execTime);
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            Intent intent = new Intent(...);
            startActivity(intent);
            finish();   
        }
    };
    asyncTask.execute(null, null);
}

Upvotes: 1

Rustam
Rustam

Reputation: 6515

you should implement AsyncTask so that this will wait untill your syncFromInternet() is completed.

    private class BackgroundSplashTask extends AsyncTask {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();

               // show progressbar            

            }

            @Override
            protected Object doInBackground(Object[] params) {
                try {

                        syncFromInternet(); // sync in background
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                return null;
            }

            @Override
            protected void onPostExecute(Object o) {
                super.onPostExecute(o);
         // dismiss progressbar     
                Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
                startActivity(intent);  // go to SecondActivity after syncFromInternet is completed
                finish();

            }

       }

Upvotes: 0

Related Questions