case R.id.btn5:
//为什么这里 不需要handler呢
Log.i("ThreadTest", Thread.currentThread().getName());
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setIcon(R.drawable.icon);
progressDialog.setMax(100);
progressDialog.setTitle("loading");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//progressDialog.setSecondaryProgress(100);
progressDialog.show();
//又开了一个子线程
new Thread(){
public void run() {
int progress =0;
while(progress++<=100){
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
progressDialog.incrementProgressBy(1);
progressDialog.incrementSecondaryProgressBy(2);
}
Log.i("ThreadTest", Thread.currentThread().getName());
progressDialog.dismiss();
//textView.setText("xiazai wan bi");
};
}.start();
break;
在子线程中:progressDialog调用incrementProgressBy() 这个方法 竟然能更新 ProgressDialog 这是什么情况。。不是说只有主线程才能更新界面吗。?望高人点拨一下。是不是我对Activity概念没有弄清楚。。
6 个解决方案
#1
progressDialog.incrementProgressBy(1);
把这个换成progressDialog.setProgress(progressDialog.getProgress()+1)看看
把这个换成progressDialog.setProgress(progressDialog.getProgress()+1)看看
#2
这个要看源码是怎么更新的了,首先判断mUiThreadId == Thread.currentThread().getId()) 是不是ui线程是则直接刷新,不是的话是用Post来刷新的不是直接刷。所以概念上是没冲突的。
private synchronized void refreshProgress(int id, int progress, boolean fromUser) {
if (mUiThreadId == Thread.currentThread().getId()) {
doRefreshProgress(id, progress, fromUser);
} else {
RefreshProgressRunnable r;
if (mRefreshProgressRunnable != null) {
// Use cached RefreshProgressRunnable if available
r = mRefreshProgressRunnable;
// Uncache it
mRefreshProgressRunnable = null;
r.setup(id, progress, fromUser);
} else {
// Make a new one
r = new RefreshProgressRunnable(id, progress, fromUser);
}
post(r);
}
}
#3
谢谢1-2楼
#4
2楼的说的有道理
#6
顶
#1
progressDialog.incrementProgressBy(1);
把这个换成progressDialog.setProgress(progressDialog.getProgress()+1)看看
把这个换成progressDialog.setProgress(progressDialog.getProgress()+1)看看
#2
这个要看源码是怎么更新的了,首先判断mUiThreadId == Thread.currentThread().getId()) 是不是ui线程是则直接刷新,不是的话是用Post来刷新的不是直接刷。所以概念上是没冲突的。
private synchronized void refreshProgress(int id, int progress, boolean fromUser) {
if (mUiThreadId == Thread.currentThread().getId()) {
doRefreshProgress(id, progress, fromUser);
} else {
RefreshProgressRunnable r;
if (mRefreshProgressRunnable != null) {
// Use cached RefreshProgressRunnable if available
r = mRefreshProgressRunnable;
// Uncache it
mRefreshProgressRunnable = null;
r.setup(id, progress, fromUser);
} else {
// Make a new one
r = new RefreshProgressRunnable(id, progress, fromUser);
}
post(r);
}
}
#3
谢谢1-2楼
#4
2楼的说的有道理
#5
#6
顶