TShopping

 找回密碼
 註冊
搜索
查看: 1297|回復: 0
打印 上一主題 下一主題

[教學] Android 如何同步下使用 Progress Dialog

[複製鏈接]
跳轉到指定樓層
1#
發表於 2018-8-30 12:28:07 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
問題是這樣的我有兩個activities 分別是Client1.java and Client2.java
Client1創建tcp/ip連線到遠端伺服器去執行指令,伺服器將回傳結果到Client1,結果將會在Client2秀出,我使用progreesDialog在Client1 Activity表示遠端伺服器的傳輸狀況,讓我們明瞭所在進度。

但目前看到的文章都是在描述非同步傳輸狀態下使用progreesDialog
我需要的是同步狀態下使用progreesDialog,伺服器把傳輸狀態回傳到Client2
找了很多文章,發現有一篇在寫這個的

舉例:
  • onPreExecute() - where you display Progress dialog.
  • doInBackground() - Where you need to write long running task, remote call logic goes here.
  • onPostExecute() - Where you need to write coding for starting Client2 activity and also dismiss dialog

代碼如下:
  1. private class SessionTask extends AsyncTask<String, Integer, Integer> {

  2.     ProgressDialog dialog;

  3.     @Override
  4.     protected void onPreExecute() {
  5.         super.onPreExecute();

  6.         dialog = new ProgressDialog(TestUI.this);
  7.         dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  8.         dialog.setTitle("UploadFile");
  9.         dialog.setMessage("Uploading file...");
  10.         dialog.setCancelable(false);
  11.         dialog.setProgress(0);
  12.         dialog.show();
  13.     }

  14.     @Override
  15.     protected Integer doInBackground(String... params) {
  16.         .........
  17.         } catch(MalformedURLException e) {
  18.             Log.e(TestUI.TAG, "E: Malformed URL! " + e.getLocalizedMessage());

  19.             return 1;
  20.         } catch(IOException e) {
  21.             Log.e(TestUI.TAG, "E: I/O error! " + e.getLocalizedMessage());
  22.         return 2;
  23.         }
  24.         return 0;
  25.     }

  26.     @Override
  27.     protected void onProgressUpdate(Integer... values) {
  28.         super.onProgressUpdate(values);
  29.         dialog.setMax(values[1]);
  30.         dialog.setProgress(values[0]);

  31.     }

  32.     @Override
  33.     protected void onPostExecute(Integer result) {
  34.         super.onPostExecute(result);
  35.         dialog.dismiss();
  36.         switch (result) {
  37.         case 0:
  38.             Toast.makeText(TestUI.this, "Uploading finished", Toast.LENGTH_LONG).show();

  39.             new DownloadTask().execute(new String[] {TestUI.LINK_DOWN, TestUI.FILE_DOWN});
  40.             break;
  41.         case 1:
  42.             Toast.makeText(TestUI.this, "E: Malformed URL!", Toast.LENGTH_LONG).show();
  43.             break;
  44.         case 2:
  45.             Toast.makeText(TestUI.this, "E: I/O error! Connection was dismissed!!!", Toast.LENGTH_LONG).show();

  46.             break;
  47.         }
  48.     }
  49. }
複製代碼





 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-26 11:53 , Processed in 0.059609 second(s), 22 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表