標籤:
子線程不能直接操作主線程 UI線程
//水平進度條 public void jdt1_onclick(View view) { final ProgressDialog pd = new ProgressDialog(this); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setMessage("正在載入請稍後"); pd.show(); //建立thread執行個體 實現Runable介面 啟動多線程 new Thread(new Runnable() { @Override public void run() { for (int i = 0; i <= pd.getMax(); i++) { try { Thread.sleep(100); } catch (Exception e) { } pd.setProgress(i); } pd.dismiss(); } }).start(); //建立thread執行個體 重寫run方法 啟動多線程 new Thread() { @Override public void run() { super.run(); //新線程 子線程 for (int i = 0; i <= pd.getMax(); i++) { try { Thread.sleep(100); } catch (Exception e) { } pd.setProgress(i); } pd.dismiss();//關閉 //子線程不能直接操作主線程 UI線程 //1.可以用hander //2.也可以用runOnUiThread runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(UIActivity2.this, "載入完成", Toast.LENGTH_SHORT).show(); } }); } }.start(); }
Android——子線程操作主線程