我有以下三个类:
当我在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)
{
}
}
}
答案 0 :(得分:2)
问题是ProgressDialog
始终需要当前活动上下文才能显示。但在您的情况下,ProgressDialog
很少不幸
原因是,只要您点击ProgressDialog
下一行,就会从当前活动中取出上下文并启动下一个活动,即FirstActivity
。所以您的progressDialog
没有机会呈现本身在屏幕上。
答案 1 :(得分: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。我不会删除这个答案(除非你想要我),因为这是你做你想做的另一种方式。
答案 2 :(得分:1)
try
{
wt.join();
Intent ActivityIntent = getIntent();
setResult(RESULT_OK, ActivityIntent);
finish();
}
这是你的问题。 UI线程正在等待工作线程完成join()
。