`

定時線程(Timer)操作UI界面

阅读更多

需求:先彈出dialog,然后,隔一定時間,修改dialog的msg.

如果只是dismiss dialog 并不需要透過Handle

 

runOnUiThread 如果開啟新的定時線程,不起作用.

 

android的線程通訊透過handle來處理,這里也一樣

 

 

 public static void showProgressDialog2(Context context, String title,
   String msg, boolean cancelable, final long runtime, int iconId,
   final String title2, final String msg2, final long closetime) {
  final ProgressDialog mypDialog = new ProgressDialog(context);
  mypDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); //
  // 设置ProgressDialog 标题
  mypDialog.setTitle(title); // 设置ProgressDialog 提示信息
  mypDialog.setMessage(msg); // 设置ProgressDialog 标题图标
  mypDialog.setIcon(iconId); // 设置ProgressDialog 的进度条是否不明确
  mypDialog.setIndeterminate(false); // 设置ProgressDialog 是否可以按退回按键取消
  mypDialog.setCancelable(cancelable); // 让ProgressDialog 显示
  mypDialog.show();
  final Handler currentHandler = new EHandler(Looper.getMainLooper(),mypDialog);
  // Activity activity = (Activity) context;
  // activity.runOnUiThread(new Runnable() {// 對UI的操作放在uiThread里面
  // public void run() {
  // };
  // });
  new Timer().schedule(new TimerTask() {
   @Override
   public void run() {
//    mypDialog.setTitle(title2); //
    // 设置ProgressDialog 提示信息
//    mypDialog.setMessage(msg2);
    Message msg= currentHandler.obtainMessage(1,1,1,msg2);
    currentHandler.sendMessage(msg);
    new Timer().schedule(new TimerTask() {
     @Override
     public void run() {
      mypDialog.dismiss();
     }
    }, closetime);
   }
  }, runtime);

 }

 static class EHandler extends Handler {
  private ProgressDialog progressDialog;
  public EHandler(Looper looper){
   super(looper);
  }
  public EHandler(Looper looper, ProgressDialog dialog) {
   super(looper);
   progressDialog = dialog;
  }
  @Override
  public void handleMessage(Message msg) {
   progressDialog.setMessage(msg.obj.toString());
  }
 }

參考http://www.iteye.com/wiki/topic/667340文章,runOnUiThread 正確做法是在里面修改ui,而不能另開線程.我估計它的背后也就是利用了handle在處理.

代碼如下:

 public static void showProgressDialog2(Context context, String title,
   String msg, boolean cancelable, final long runtime, int iconId,
   final String title2, final String msg2, final long closetime) {
  final ProgressDialog mypDialog = new ProgressDialog(context);
  mypDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); //
  // 设置ProgressDialog 标题
  mypDialog.setTitle(title); // 设置ProgressDialog 提示信息
  mypDialog.setMessage(msg); // 设置ProgressDialog 标题图标
  mypDialog.setIcon(iconId); // 设置ProgressDialog 的进度条是否不明确
  mypDialog.setIndeterminate(false); // 设置ProgressDialog 是否可以按退回按键取消
  mypDialog.setCancelable(cancelable); // 让ProgressDialog 显示
  mypDialog.show();
  // final Handler currentHandler = new
  // EHandler(Looper.getMainLooper(),mypDialog);
  final Activity activity = (Activity) context;

  new Timer().schedule(new TimerTask() {
   @Override
   public void run() {
    // Message msg= currentHandler.obtainMessage(1,1,1,msg2);
    // currentHandler.sendMessage(msg);
    // 試試用runOnUiThread方法
    activity.runOnUiThread(new Runnable() {// 對UI的操作放在uiThread里面
       public void run() {
        mypDialog.setMessage(msg2);
        mypDialog.setTitle(title2);
       };
      });
    new Timer().schedule(new TimerTask() {
     @Override
     public void run() {
      mypDialog.dismiss();
     }
    }, closetime);
   }
  }, runtime);

 }

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics