Java 如何每2秒更改ProgressDialog的消息?
我有ProgressDialog,我想每2秒更改一次消息。在这段代码中,我将进度时间设置为10秒。所以我想让它有5条信息Java 如何每2秒更改ProgressDialog的消息?,java,android,Java,Android,我有ProgressDialog,我想每2秒更改一次消息。在这段代码中,我将进度时间设置为10秒。所以我想让它有5条信息 在代码中使用Timer,并设置schedule方法的时间 在计划中方法 第一个参数是TimerTask 第二个参数是延迟时间 第三个参数是期间时间 试试这个 private void showProgressDialog() { progressDialog = new ProgressDialog(this, getProgressDailogStyle())
在代码中使用
Timer
,并设置schedule
方法的时间
在计划中
方法
- 第一个参数是
TimerTask
- 第二个参数是
时间延迟
- 第三个参数是
时间期间
private void showProgressDialog() {
progressDialog = new ProgressDialog(this, getProgressDailogStyle());
progressDialog.setMessage("Processing ...");
progressDialog.show();
progressDialog.setCancelable(false);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// do something
Message message=new Message();
message.what=0;
mHandler.sendMessage(message);
}
}, 0, 2000);
}
更新
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if(msg.what == 0){
//change message
}
}
};
请尝试下面的代码,并且setMessage必须在runOnUiThread中
public void setMessage(String message) {
if (dialog != null && dialog.isShowing()) {
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.setMessage(message);
}
});
}
}
你可以用
由于您已经在运行处理程序,因此很容易将消息显示到UI线程,即ProgressDialog。不要在循环中运行处理程序10秒,而是以5秒间隔运行它,即像这样延迟2秒
private int count = 1;
Runnable runnable = null;
private void showProgressDialog() {
progressDialog = new ProgressDialog(this, getProgressDailogStyle());
progressDialog.setMessage("Processing ...");
progressDialog.show();
progressDialog.setCancelable(false);
final Handler handler = new Handler();
runnable = new Runnable() {
public void run() {
Log.i("message",""+counter);
if (counter == 5) {
handler.removeCallbacks(runnable);
progressDialog.dismiss();
// mInterstitialAd.show();
}else {
progressDialog.setMessage("your message");
handler.postDelayed(runnable, 2000);
counter++;
}
}
};
handler.post(runnable);
}
我把新消息放在哪里?您可以在
处理程序中更改消息。祝您好运。@devonjoe很高兴听到这个消息。
public void setMessage(String message) {
if (dialog != null && dialog.isShowing()) {
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.setMessage(message);
}
});
}
}
count = 0;
progressDialog = new ProgressDialog(this, getProgressDailogStyle());
progressDialog.setMessage("Processing ...");
progressDialog.show();
progressDialog.setCancelable(false);
new CountDownTimer(10000, 2000) {
public void onTick(long millisUntilFinished) {
//here you can have your logic to set message
count=count+1;
if (count==1){
progressDialog.setMessage("Processing 1");
}else if (count==2){
progressDialog.setMessage("Processing 2");
}
// until the count = 5
}
public void onFinish() {
//the progress is finish
count = 0;
progressDialog.dismiss();
}
}.start();
private int count = 1;
Runnable runnable = null;
private void showProgressDialog() {
progressDialog = new ProgressDialog(this, getProgressDailogStyle());
progressDialog.setMessage("Processing ...");
progressDialog.show();
progressDialog.setCancelable(false);
final Handler handler = new Handler();
runnable = new Runnable() {
public void run() {
Log.i("message",""+counter);
if (counter == 5) {
handler.removeCallbacks(runnable);
progressDialog.dismiss();
// mInterstitialAd.show();
}else {
progressDialog.setMessage("your message");
handler.postDelayed(runnable, 2000);
counter++;
}
}
};
handler.post(runnable);
}