ProgressDialog显示了线程完成后(ProgressDialog shows up aft

2019-10-16 21:50发布

我有以下三类:

当我试图表明,当WorkingThread正在运行progressDialog的ProgressDialog只有WorkingThread完成后显示出来。 我究竟做错了什么?

我没有兴趣使用的AsyncTask!

-StartActivity:

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"
        }
    }
}

-

-FirstActivity:

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) 
        {
        }
    }
}

-WorkingThread:

public class WorkingThread extends Thread 
{

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

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

Answer 1:

问题是ProgressDialog总是需要display.But 当前活动的上下文 ,你的情况ProgressDialog是有点可惜

究其原因,一旦是你火ProgressDialog接下来的几行从当前活动采取断章取义,并开始下一个活动即FirstActivity 。所以你progressDialog变得没有机会展示自己在屏幕上。



Answer 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您的编辑。 (除非你要我),因为它是做你想做的事的另一种方式,我不会删除这个答案。



Answer 3:

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

这里是你的问题。 该UI线程正在等待工作线程来完成的becaouse的join()



文章来源: ProgressDialog shows up after thread is done