本文介绍了线程完成后显示 ProgressDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下三个类:

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

When I try to show a progressDialog when the WorkingThread is running, the ProgressDialog only shows up after the WorkingThread is done. What am I doing wrong?

我对使用 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)
        {
        }
    }
}

推荐答案

问题是 ProgressDialog 总是需要 current Activity context 来显示.但在你的情况下 ProgressDialog 有点不幸

The problem is ProgressDialog always need current Activity context for display.But in your case ProgressDialog is little unfortunate

原因是一旦你触发ProgressDialog,接下来的几行就会从当前活动中取出上下文并启动下一个活动,即FirstActivity.所以你的progressDialog 没有机会在屏幕上展示自己.

The reason is as soon as you fire ProgressDialog the next couple of lines take out Context from Current activity and starts Next Activity i.e FirstActivity.So your progressDialog gets no chance to present itself on the Screen.

这篇关于线程完成后显示 ProgressDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 22:23