ProgressDialog 即使使用线程也不起作用
ProgressDialog does not work even using thread
我是 Android 开发新手。我正在尝试显示 ProgressDialog
。我看到很多教程说显示对话框必须使用线程。如您所见,片段代码正在使用线程。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
refreshFromFeed();
} catch (InterruptedException e) {
e.printStackTrace();
}
setContentView(R.layout.activity_main);
}
private void refreshFromFeed() throws InterruptedException {
ProgressDialog dialog = ProgressDialog.show(this,"Loading","Wake up after some sleep");
Thread th = new Thread(){
public void run(){
Log.d("TimeFrom", String.valueOf(System.currentTimeMillis()/1000));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("TimeTo", String.valueOf(System.currentTimeMillis()/1000));
}
};
th.start();
dialog.dismiss();
}
protected void onRefresh(View view) throws InterruptedException {
refreshFromFeed();
}
日志显示耗时 5 秒,但是,我在屏幕上看不到任何对话框,但我可以在屏幕上执行任何操作。即使我在物理设备上使用。我用过调试模式。也不例外。
onRefresh
是 onClick
在 xml 上声明的事件。
我对您的代码做了一些修改,请仔细阅读。
private void refreshFromFeed() throws InterruptedException {
ProgressDialog dialog = ProgressDialog.show(this,"Loading","Wake up after some sleep");
Thread th = new Thread(){
public void run(){
Log.d("TimeFrom", String.valueOf(System.currentTimeMillis()/1000));
try {
Thread.sleep(5000);
dialog.dismiss();
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("TimeTo",String.valueOf(System.currentTimeMillis()/1000));
}
};
th.start();
}
您在显示对话框后立即将其关闭。
也许您想将 "dialog.dismiss();" 移到线程内部。请记住,您需要关闭 UI 线程上的对话框,否则它会使您的应用程序崩溃:
private void refreshFromFeed() throws InterruptedException {
final ProgressDialog dialog = ProgressDialog.show(this,"Loading","Wake up after some sleep");
Thread th = new Thread(){
public void run(){
Log.d("TimeFrom", String.valueOf(System.currentTimeMillis()/1000));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("TimeTo", String.valueOf(System.currentTimeMillis()/1000));
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.dismiss();
}
});
}
};
th.start();
}
I see lots of tutorial that say for showing dialog must use thread.
您不需要一个线程来显示 ProgressDialog,这只是一个在 5000 毫秒后关闭它的示例
private void refreshFromFeed() throws InterruptedException {
final ProgressDialog dialog = ProgressDialog.show(getActivity(),"Loading","Wake up after some sleep");
Thread th = new Thread() {
public void run() {
Log.d("TimeFrom", String.valueOf(System.currentTimeMillis() / 1000));
try {
Thread.sleep(5000);
dialog.dismiss(); // dismiss your dialog here
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("TimeTo", String.valueOf(System.currentTimeMillis() / 1000));
}
};
th.start();
}
您的 UI 线程中仍然 运行 ProgressDialog。
ProgressDialog dialog = ProgressDialog.show(this,"Loading","Wake up after some sleep");
新话题是在这一行之后创建的,而不是在这一行之前!
我是 Android 开发新手。我正在尝试显示 ProgressDialog
。我看到很多教程说显示对话框必须使用线程。如您所见,片段代码正在使用线程。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
refreshFromFeed();
} catch (InterruptedException e) {
e.printStackTrace();
}
setContentView(R.layout.activity_main);
}
private void refreshFromFeed() throws InterruptedException {
ProgressDialog dialog = ProgressDialog.show(this,"Loading","Wake up after some sleep");
Thread th = new Thread(){
public void run(){
Log.d("TimeFrom", String.valueOf(System.currentTimeMillis()/1000));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("TimeTo", String.valueOf(System.currentTimeMillis()/1000));
}
};
th.start();
dialog.dismiss();
}
protected void onRefresh(View view) throws InterruptedException {
refreshFromFeed();
}
日志显示耗时 5 秒,但是,我在屏幕上看不到任何对话框,但我可以在屏幕上执行任何操作。即使我在物理设备上使用。我用过调试模式。也不例外。
onRefresh
是 onClick
在 xml 上声明的事件。
我对您的代码做了一些修改,请仔细阅读。
private void refreshFromFeed() throws InterruptedException {
ProgressDialog dialog = ProgressDialog.show(this,"Loading","Wake up after some sleep");
Thread th = new Thread(){
public void run(){
Log.d("TimeFrom", String.valueOf(System.currentTimeMillis()/1000));
try {
Thread.sleep(5000);
dialog.dismiss();
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("TimeTo",String.valueOf(System.currentTimeMillis()/1000));
}
};
th.start();
}
您在显示对话框后立即将其关闭。 也许您想将 "dialog.dismiss();" 移到线程内部。请记住,您需要关闭 UI 线程上的对话框,否则它会使您的应用程序崩溃:
private void refreshFromFeed() throws InterruptedException {
final ProgressDialog dialog = ProgressDialog.show(this,"Loading","Wake up after some sleep");
Thread th = new Thread(){
public void run(){
Log.d("TimeFrom", String.valueOf(System.currentTimeMillis()/1000));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("TimeTo", String.valueOf(System.currentTimeMillis()/1000));
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.dismiss();
}
});
}
};
th.start();
}
I see lots of tutorial that say for showing dialog must use thread.
您不需要一个线程来显示 ProgressDialog,这只是一个在 5000 毫秒后关闭它的示例
private void refreshFromFeed() throws InterruptedException {
final ProgressDialog dialog = ProgressDialog.show(getActivity(),"Loading","Wake up after some sleep");
Thread th = new Thread() {
public void run() {
Log.d("TimeFrom", String.valueOf(System.currentTimeMillis() / 1000));
try {
Thread.sleep(5000);
dialog.dismiss(); // dismiss your dialog here
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("TimeTo", String.valueOf(System.currentTimeMillis() / 1000));
}
};
th.start();
}
您的 UI 线程中仍然 运行 ProgressDialog。
ProgressDialog dialog = ProgressDialog.show(this,"Loading","Wake up after some sleep");
新话题是在这一行之后创建的,而不是在这一行之前!