一个线程完成执行后,在android中运行一个线程

时间:2014-11-09 11:04:41

标签: android multithreading android-asynctask

我希望在一个线程完成执行后运行一个线程。

这里我使用进度条,进度条完成后方法do1()应该执行但是当我运行应用程序时应用程序强制关闭。

这是我的代码..

         public void onenc(View view) {
          progressBar = new ProgressDialog(view.getContext());
        progressBar.setCancelable(true);
        progressBar.setMessage("Ecoding Text ...");
        progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressBar.setProgress(0);
        progressBar.setMax(100);
        progressBar.show();

        //reset progress bar status
        progressBarStatus = 0;

        //reset filesize
        fileSize = 0;


        new Thread(new Runnable() {
          public void run() {
            while (progressBarStatus < 100) {

              // process some tasks
              progressBarStatus = doSomeTasks();

              // your computer is too fast, sleep 1 second
              try {
                Thread.sleep(10);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }

              // Update the progress bar
              progressBarHandler.post(new Runnable() {
                public void run() {
                  progressBar.setProgress(progressBarStatus);
                }
              });
            }

            // ok, file is downloaded,
            if (progressBarStatus >= 100) {

                // sleep 2 seconds, so that you can see the 100%
                try {
                    Thread.sleep(2000);

                } catch (Exception e) {
                    e.printStackTrace();
                }

                // close the progress bar dialog
                progressBar.dismiss();

            }


          }
           }).start();

        Thread tt =new Thread(new Runnable() {
              public void run() {
        do1();

         try {
                Thread.sleep(1100);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }

              }
           });
        tt.start();

      }

我也尝试过AsyncTask,但两者同时运行。

请帮帮我,我是android的新手。

提前致谢。

更新

AsyncTask之后

      class MyFirstTask extends AsyncTask<String, Boolean, Boolean> {
            @Override
            protected Boolean doInBackground(String... params) {
                return null;
                //Do Stuff
            }
           public void progressUpdate(Integer progress) {
               new Thread(new Runnable() {
                      public void run() {
                        while (progressBarStatus < 100) {

                          // process some tasks
                          progressBarStatus = doSomeTasks();

                          // your computer is too fast, sleep 1 second
                          try {
                            Thread.sleep(10);
                          } catch (InterruptedException e) {
                            e.printStackTrace();
                          }

                          // Update the progress bar
                          progressBarHandler.post(new Runnable() {
                            public void run() {
                              progressBar.setProgress(progressBarStatus);
                            }
                          });
                        }

                        // ok, file is downloaded,
                        if (progressBarStatus >= 100) {

                            // sleep 2 seconds, so that you can see the 100%
                            try {
                                Thread.sleep(2000);

                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            // close the progress bar dialog
                            progressBar.dismiss();

                        }


                      }
                       }).start();
           }
           @Override
           protected void onPostExecute(Boolean result) {
               //Call your next task
               Thread tt =new Thread(new Runnable() {
                  public void run() {
            do1();

             try {
                    Thread.sleep(1100);
                  } catch (InterruptedException e) {
                    e.printStackTrace();
                  }

                  }
               });
            tt.start();
           }
      }

现在甚至没有一个线程正在执行应用程序强制关闭

另一次更新后

            class MyFirstTask extends AsyncTask<String, Boolean, Boolean> {
            @Override
            protected Boolean doInBackground(String... params) {
                  new Thread(new Runnable() {
                      public void run() {
                        while (progressBarStatus < 100) {

                          // process some tasks
                          progressBarStatus = doSomeTasks();

                          // your computer is too fast, sleep 1 second
                          try {
                            Thread.sleep(10);
                          } catch (InterruptedException e) {
                            e.printStackTrace();
                          }

                          // Update the progress bar
                          progressBarHandler.post(new Runnable() {
                            public void run() {
                              progressBar.setProgress(progressBarStatus);
                            }
                          });
                        }

                        // ok, file is downloaded,
                        if (progressBarStatus >= 100) {

                            // sleep 2 seconds, so that you can see the 100%
                            try {
                                Thread.sleep(2000);

                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            // close the progress bar dialog
                            progressBar.dismiss();

                        }


                      }
                       }).start();

                return null;
                //Do Stuff
            }
           public void progressUpdate(Integer progress) {

           }
           @Override
           protected void onPostExecute(Boolean result) {

               do1();
               //Call your next task
        /*     Thread tt =new Thread(new Runnable() {
                  public void run() {
            do1();

             try {
                    Thread.sleep(1100);
                  } catch (InterruptedException e) {
                    e.printStackTrace();
                  }

                  }
               });
            tt.start();*/
           }
      }

现在函数do1()在进度条完成之前执行。

终于解决了......这就是答案。 希望它能帮助别人

         class MyFirstTask extends AsyncTask<String, String, String> {
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
              progressBar.show();


          }  
          @Override
            protected String doInBackground(String... params) {


              int i=0;
                while(fileSize<100)
                {
                    fileSize=fileSize+1;
                     publishProgress(""+(int)(fileSize));
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }                       
                }

                 publishProgress(""+(int)(fileSize));

                return null;


            }
           public void onProgressUpdate(String... progress) {
               progressBar.setProgress(Integer.parseInt(progress[0]));
           }
           @Override
           protected void onPostExecute(String result) {
               progressBar.dismiss();
               do1();


           }
      }

2 个答案:

答案 0 :(得分:1)

根本不使用Thread()。使用AsyncTask,因为它使生活更轻松,并实现onPostExecute()方法来调用下一个AsyncTask

class MyFirstTask extends AsyncTask<String, Boolean, Boolean> {
    @Override
    protected Boolean doInBackground(String... params) {
        //Do Stuff that takes ages (background thread)
        for(int i=0;i<100;i++){   
            doStuff();
            Thread.sleep(1000L); //sleep because I'm just tired
            publishProgress(i);
            Thread.sleep(2000L); //sleep some more
        }
    }
   @Override
   public void progressUpdate(Integer progress) {
       //Update progress bar (ui thread)
       progressBar.setProgress(progress);

   }
   @Override
   protected void onPostExecute(Boolean result) {
       //Call your next task (ui thread)
       new MyNextTask().execute();
   }

开始你的第一个任务

new MyFirstTask().execute();

答案 1 :(得分:0)

您可以简单地使用join()方法。

Thread first = new Thread();
Thread secThread =new Thread();
first.start();
first.join();
secThread.start();

但不要在主线程上执行此操作。