【问题标题】:Pressing Button and continuing to next Intent, ProgressDialog按下按钮并继续下一个意图,ProgressDialog
【发布时间】:2013-10-21 13:16:00
【问题描述】:

我想在我的应用上传图片时显示 ProgressDialog。 因为上传图片可能需要相当长的时间。 现在我有 2 个异步任务,一个用于将图像上传到服务器,一个用于发送电子邮件。发送电子邮件后,应该会弹出下一个意图。

这是我下一个按钮的代码:

public void next(View view) {
    Intent intent = new Intent(Step4.this, Step5.class);

    intent.putExtra(EXTRA_MESSAGE, (Serializable) _user);
    intent.putExtra(EXTRA_MESSAGE2, _isRepairable);
    intent.putExtra(EXTRA_MESSAGE3, _injury);
    intent.putExtra(EXTRA_MESSAGE4, _category);
    intent.putExtra(EXTRA_MESSAGE5, _inch);
    intent.putExtra(EXTRA_MESSAGE6, _size);
    mailing(_isRepairable);
    new UploadImageTask().execute();
    startActivity(intent);
}

这是我的 UploadImageTask()

private class UploadImageTask extends AsyncTask<Void, Void, Integer> {
    ProgressDialog dialog;
    StopWatch sw = new StopWatch();
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(Step4.this);
        dialog.show();
    }

    protected Integer doInBackground(Void... params) {
        if (isPhotoTaken()) {
            sw.start();
            ImageUploader.uploadFile(getPhotoPath(),
                    "http://obo.nl/android-upload-image.php", Step4.this);
            sw.stop();
            Debug.out(sw.getTime());
        }
        return null;
    }

    protected void onPostExecute(Integer result) {
        dialog.dismiss();
        new MyAsyncTask().execute(_mail);
    }
}

这是我的 MyAsyncTask

private class MyAsyncTask extends AsyncTask<Mail, Integer, Double> {
    /**
     * Private boolean to check if a tire is repairable or not.</br>
     */
    boolean _repairable = Step4._isRepairable;

    /**
     * Private integer which counts how many times we've tried to send the
     * Email.
     */
    private int _counter = 0;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    /**
     * Method used to start {@link #postData(Mail)} on a background thread.
     * 
     * @return null
     */
    @Override
    protected Double doInBackground(Mail... params) {
        postData(params[0]);
        return null;
    }

    /**
     * Method used to send the mail through a JSON Request in combination
     * with the website. If there is no Internet connection the program will
     * try to send the mail every 10 seconds.
     * 
     * @param valueIWantToSend
     */
    public void postData(Mail valueIWantToSend) {
        if (AppStatus.haveNetworkConnection(_context)) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://obo.nl/android-mailing.php");
            try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("from",
                        valueIWantToSend.getFrom()));
                nameValuePairs.add(new BasicNameValuePair("to",
                        valueIWantToSend.getTo()));
                nameValuePairs.add(new BasicNameValuePair("subject",
                        valueIWantToSend.getSubject()));
                nameValuePairs.add(new BasicNameValuePair("message",
                        valueIWantToSend.getBody()));
                nameValuePairs.add(new BasicNameValuePair("localized",
                        getResources().getConfiguration().locale
                                .getDisplayName()));
                if (PathToPDF(_repairable).contains("Goed_Gekeurd_NL")) {
                    nameValuePairs.add(new BasicNameValuePair(
                            "outputResult",
                            SERVER_BANDENANALYSE_GOED_GEKEURD_PATH));
                } else if (PathToPDF(_repairable).contains("Afgekeurd_NL")) {
                    nameValuePairs.add(new BasicNameValuePair(
                            "outputResult",
                            SERVER_BANDENANALYSE_AFGEKEURD_PATH));
                } else if (PathToPDF(_repairable).contains(
                        "Goed_Gekeurd_FR")) {
                    nameValuePairs.add(new BasicNameValuePair(
                            "outputResult",
                            SERVER_ANALYSEPNEUS_GOED_GEKEURD_PATH));
                } else if (PathToPDF(_repairable).contains("Afgekeurd_FR")) {
                    nameValuePairs.add(new BasicNameValuePair(
                            "outputResult",
                            SERVER_ANALYSEPNEUS_AFGEKEURD_PATH));
                } else if (PathToPDF(_repairable).contains(
                        "Goed_Gekeurd_DE")) {
                    nameValuePairs.add(new BasicNameValuePair(
                            "outputResult",
                            SERVER_REIFENANALYSE_GOED_GEKEURD_PATH));
                } else if (PathToPDF(_repairable).contains("Afgekeurd_DE")) {
                    nameValuePairs.add(new BasicNameValuePair(
                            "outputResult",
                            SERVER_REIFENANALYSE_AFGEKEURD_PATH));
                } else if (PathToPDF(_repairable).contains(
                        "Goed_Gekeurd_EN")) {
                    nameValuePairs.add(new BasicNameValuePair(
                            "outputResult",
                            SERVER_TYREANALYSE_GOED_GEKEURD_PATH));
                } else if (PathToPDF(_repairable).contains("Afgekeurd_EN")) {
                    nameValuePairs.add(new BasicNameValuePair(
                            "outputResult",
                            SERVER_TYREANALYSE_AFGEKEURD_PATH));
                }
                if (isPhotoTaken()) {
                    nameValuePairs.add(new BasicNameValuePair("photo",
                            getPhotoPath()));
                }
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                String response = httpclient.execute(httppost,
                        responseHandler);

                // This is the response from a php application
                String reverseString = response;
                Log.i("info", reverseString);

            } catch (ClientProtocolException e) {

            } catch (IOException e) {

            }
        } else {
            if (_counter == 0) {
                _counter++;
                _activity.runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(_context,
                                getString(R.string.noInternetEmailNotSend),
                                Toast.LENGTH_LONG).show();
                    }
                });

            }
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            postData(valueIWantToSend);
        }
    }

}

那么我怎样才能让它在上传时显示 ProgressDialog(现在它显示几秒钟(比如 3 秒),而上传需要 15 - 20 秒。 对话框应该持续 20 秒而不是仅 3 秒。 此外,当对话框关闭时,它已经进入下一个意图。虽然它实际上应该在 20 秒而不是 3 秒内进入下一个意图。

【问题讨论】:

  • 在Async task1的post execute中先上传你的图片(Async task1),你可以调用intent(Async Task2)进行邮件上传吗??

标签: android multithreading android-intent progressdialog stopwatch


【解决方案1】:

不要关闭 UploadImageTask

中的对话框
protected void onPostExecute(Integer result) {
       // dialog.dismiss();
        new MyAsyncTask().execute(_mail);
    }

但在 MyAsyncTask

中忽略相同的内容
 protected void onPostExecute(Integer result) {
  if (dialog.isShowing()) 
    {
           dialog.dismiss();
    }
 }

【讨论】:

    【解决方案2】:

    当您使用new MyAsyncTask().execute(_mail) 执行 MyAsyncTask 时,您可以在 MyAsyncTask 中传递步骤 4 的进度对话框

    MyAsyncTask

        private class MyAsyncTask extends AsyncTask<Mail, Integer, Double> {
    
        private ProgressDialog progress = null
    
    // create constructor to get ProgressDialog passed from onPostExecute of your UploadImageTask
    
            public MyAsyncTask (ProgressDialog progress)
            {
            this.progress = progress;
            }
    
        // now continue `this.progress` here and dismiss `this.progress` on `onPostExecute` method here
        } 
    

    您的 UploadImageTask 的 onPostExecute

    protected void onPostExecute(Integer result)
     {
           // remove dialog.dismiss(); // dont dismiss dialog here.
    
            MyAsyncTask async = new MyAsyncTask(dialog) // pass your current dialog
            async.execute(_mail);
     }
    

    抱歉有任何错别字。希望这会有所帮助

    【讨论】:

      猜你喜欢
      • 2019-06-13
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      • 2016-12-05
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      相关资源
      最近更新 更多