两个Asynctask和1个splashscreen

时间:2014-04-18 10:01:53

标签: java android android-asynctask

大家好我有2个asynctask和1个splashscreen。我想在splashscreen中执行2 asynctask ... 当2个线程asynctask完成时显示我的应用程序...

arrivi.java

public class arrivi extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View arrivi = inflater.inflate(R.layout.arrivi, container, false); 


      new MyTask().execute("");

partenze.java

 public class partenze extends Fragment {

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View partenze = inflater.inflate(R.layout.partenze, container, false);
       new MyTask().execute("");
          return partenze;
   }

溅射屏幕:

public class SplashScreen extends Activity {

private boolean mIsBackButtonPressed;
private static final int SPLASH_DURATION = 6000; //6 seconds
private Handler myhandler;

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.splashscreen);

//CONTROLLO CONNESSIONE
    if (!isOnline()){
        try {
            AlertDialog alertDialog = new AlertDialog.Builder(this).create();

            alertDialog.setTitle("Info");
            alertDialog.setMessage("Connessione Internet non disponibile.");
            alertDialog.setButton("Esci", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }                       

            });
            alertDialog.show();
        }
        catch(Exception e) { }
    }
    // FINE CONTROLLO CONNESSIONE

    myhandler = new Handler();

    // run a thread to start the home screen
    myhandler.postDelayed(new Runnable()
    {
        @Override
        public void run() 
        {
           finish();

           if (!mIsBackButtonPressed)
           {
                // start the home activity 
                Intent intent = new Intent(SplashScreen.this, MainActivity.class);
                SplashScreen.this.startActivity(intent);
           }

        }

    }, SPLASH_DURATION); 
}


 //handle back button press
 @Override
 public void onBackPressed() 
 {
    mIsBackButtonPressed = true;
    super.onBackPressed();
}
你可以帮帮我吗? 什么是最简单的方法? 我希望在asynctask完成时显示我的MainActivity ...谢谢!

2 个答案:

答案 0 :(得分:0)

  1. 在启动画面类中维护两个布尔值,每个异步任务一个。
  2. 为每个asynctask覆盖onPostExecute()方法,并通过此方法更新布尔值的状态。这样您就可以检查异步任务是否完成。
  3. 继续检查while循环中的那些布尔值。

    do {
         // Do nothing. Just wait untill both booleans are set. 
    } while(bool1 && bool2);
    // Execute your code which you want to execute after finishing both async tasks
    

答案 1 :(得分:0)

AsyncTask在完成后提供执行一些代码。 http://developer.android.com/reference/android/os/AsyncTask.html#onPostExecute(Result)

您可以将有关已完成任务的信息发送到方法(例如controllerOfTaskExecution),这将启动您的MainActivity。