1

我有以下三个课程:

当我在 WorkingThread 运行时尝试显示 progressDialog 时,ProgressDialog 仅在 WorkingThread 完成后显示。我究竟做错了什么?

我对使用 AsyncTask 不感兴趣!

-开始活动:

public class StartActivity extends Activity implements OnClickListener
{
    public ProgressDialog pgd;

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

        imgv = (ImageView)findViewById(R.id.imageView1);
        tv = (TextView)findViewById(R.id.textview);

        Button btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(this);
    }

    public void onClick(View v) 
    {
        pgd = ProgressDialog.show(StartActivity.this, "", "Loading picture"); // Start ProgressDialog before starting activity

        Intent ActivityIntent = new Intent(this, FirstActivity.class);
        startActivityForResult(ActivityIntent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 0 && resultCode == RESULT_OK)
        {
            pgd.dismiss(); //Stop ProgressDialog when FirstActivity is "done"
        }
    }
}

-

-第一个活动:

public class FirstActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        WorkingThread wt = new WorkingThread();
        wt.start();

        try 
        {
            wt.join();
            Intent ActivityIntent = getIntent();
            setResult(RESULT_OK, ActivityIntent);
            finish();
        } 
        catch (Exception e) 
        {
        }
    }
}

-工作线程:

public class WorkingThread extends Thread 
{

    @Override
    public void run() 
    {
        super.run();

        try
        {
            Thread.sleep(5000);
        }
        catch (Exception e)
        {
        }
    }
}
4

3 回答 3

2

问题ProgressDialog总是需要当前的活动上下文来显示。但在你的情况下ProgressDialog有点不幸

原因是,一旦您触发ProgressDialog接下来的几行,就从 Current 活动中取出 Context 并启动 Next Activity 即。因此,FirstActivityprogressDialog没有机会在屏幕上展示自己。

于 2012-06-28T11:58:53.100 回答
2

使用AsyncTask。这是一个例子:

package com.example.test;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

ProgressDialog activityProgressDialog;
private Context context;

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

    context = this;

    Button btn = (Button)findViewById(R.id.button1);
    btn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            new TestAsyncTask().execute();
        }
    });
}

private class TestAsyncTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        //Called before doInBackground.
        //Initialize your progressDialog here.
        activityProgressDialog = ProgressDialog.show(context,
                "Test",
                "Doing heavy work on background...", false,
                false);
    }

    @Override
    protected Void doInBackground(Void... v) {

        //Do your work here
        //Important!!! Update any UI element on preExcecute and
                    //onPostExcecute not in this method or else you will get an 
                    //exception.
        //The below code just make the thread inactive for 5 seconds.
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            Thread.currentThread().notify();
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void v) {
        //Dismiss the progessDialog here.
        activityProgressDialog.dismiss();
    }
}

 }

我只是显示您不想使用 AsyncTask 的编辑。我不会删除这个答案(除非你想让我这样做),因为这是做你想做的另一种方式。

于 2012-06-28T12:13:09.083 回答
1
try 
        {
            wt.join();
            Intent ActivityIntent = getIntent();
            setResult(RESULT_OK, ActivityIntent);
            finish();
        }

这是你的问题。UI 线程正在等待工作线程完成,因为join().

于 2012-06-28T11:59:44.480 回答