問題是這樣的我有兩個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
代碼如下:
- private class SessionTask extends AsyncTask<String, Integer, Integer> {
- ProgressDialog dialog;
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- dialog = new ProgressDialog(TestUI.this);
- dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
- dialog.setTitle("UploadFile");
- dialog.setMessage("Uploading file...");
- dialog.setCancelable(false);
- dialog.setProgress(0);
- dialog.show();
- }
- @Override
- protected Integer doInBackground(String... params) {
- .........
- } catch(MalformedURLException e) {
- Log.e(TestUI.TAG, "E: Malformed URL! " + e.getLocalizedMessage());
- return 1;
- } catch(IOException e) {
- Log.e(TestUI.TAG, "E: I/O error! " + e.getLocalizedMessage());
- return 2;
- }
- return 0;
- }
- @Override
- protected void onProgressUpdate(Integer... values) {
- super.onProgressUpdate(values);
- dialog.setMax(values[1]);
- dialog.setProgress(values[0]);
- }
- @Override
- protected void onPostExecute(Integer result) {
- super.onPostExecute(result);
- dialog.dismiss();
- switch (result) {
- case 0:
- Toast.makeText(TestUI.this, "Uploading finished", Toast.LENGTH_LONG).show();
- new DownloadTask().execute(new String[] {TestUI.LINK_DOWN, TestUI.FILE_DOWN});
- break;
- case 1:
- Toast.makeText(TestUI.this, "E: Malformed URL!", Toast.LENGTH_LONG).show();
- break;
- case 2:
- Toast.makeText(TestUI.this, "E: I/O error! Connection was dismissed!!!", Toast.LENGTH_LONG).show();
- break;
- }
- }
- }
複製代碼
|