android中的线程和处理顺序正确

时间:2011-12-23 13:33:03

标签: android multithreading

我想按照正确的顺序执行以下操作: - 在显示进度对话框时执行长任务 - 然后使用此计算结果。

但是,为了显示进度对话框,我似乎被迫使用Thread。但是如果我使用一个线程,计算就在我的长任务完成之前完成。

解决方案是什么?

遗憾的是,这两种解决方案对我都不起作用。我会尽力更加彻底。 以ProfSmiles的代码为例,这就是我想要的:

final Handler mHandler = new Handler(); //create the handler
String mResults; //the results of the operation (change the type if needbe)

final Runnable mDoSomething = new Runnable() 
{
public void run()
{
    returnResultsToUi(); //what happens when it's done
}
};
protected void doActionBackground()
{
t = new Thread()
{
    public void run()
    {
        mResults = doSomethingThatTakesALongTime(); //the backbone of this entire thing
        mHandler.post(mDoSomething);
    }
};
t.start();
}
public void returnResultsToUi()
{
//you can call UI ops safely in here
try
{
    //close the spinner
    loading.setProgress(1);
    loading.dismiss();
}
catch (Exception e)
{
    Log.w("error","couldn't close loading properly");
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

loading = new ProgressDialog(this); //create and then set up the spinner
loading.setMax(1);
loading.setTitle("Loading...");
loading.setMessage("Doing something.");
loading.setCancelable(true);
loading.setIndeterminate(false);
loading.setOnCancelListener(cancelLoad()); //remember to make this method or change setCancelable(false)
loading.setProgress(0);
loading.show();

doActionBackground(); //start the background process
doSomethingAfter(mResults);

问题是当doActionBackground正在运行时调用doSomethingAfter,但mResults尚未设置且其值为null。在调用doSomethingAfter之前有没有办法强制线程完成?就像一个可以取消的函数waitTillFinished(takeALongTime)。

2 个答案:

答案 0 :(得分:3)

解决方案是使用AsyncTask和ProgressBar。

以下是example1example2

更新

+1是的,这是android中实现线程概念的确切解决方案。实施以下方案以实施您的解决方案:

  1. 在onPreExecute()方法中显示进度条。
  2. 执行doInBackground()方法中的所有后台任务。
  3. 可以在onPostExecute()方法中实现显示类型的操作。
  4. 最适合我的解释的例子是:http://www.technotalkative.com/android-load-image-from-web/,对不起我的个人博客链接,但我相信这有助于用户理解和实现AsyncTask概念。

    • 另请阅读有关进程和线程here(特别是有关AsyncTask)的内容。

答案 1 :(得分:0)

您可以使用处理程序在后台执行长时间运行的进程,并将结果返回给UI线程。例如;

final Handler mHandler = new Handler(); //create the handler
String mResults; //the results of the operation (change the type if needbe)

final Runnable mDoSomething = new Runnable() 
{
    public void run()
    {
        returnResultsToUi(); //what happens when it's done
    }
};
protected void doActionBackground()
{
    t = new Thread()
    {
        public void run()
        {
            mResults = doSomethingThatTakesALongTime(); //the backbone of this entire thing
            mHandler.post(mDoSomething);
        }
    };
    t.start();
}
public void returnResultsToUi()
{
    //you can call UI ops safely in here
    try
    {
        //close the spinner
        loading.setProgress(1);
        loading.dismiss();
    }
    catch (Exception e)
    {
        Log.w("error","couldn't close loading properly");
    }
}
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    loading = new ProgressDialog(this); //create and then set up the spinner
    loading.setMax(1);
    loading.setTitle("Loading...");
    loading.setMessage("Doing something.");
    loading.setCancelable(true);
    loading.setIndeterminate(false);
    loading.setOnCancelListener(cancelLoad()); //remember to make this method or change setCancelable(false)
    loading.setProgress(0);
    loading.show();

    doActionBackground(); //start the background process