pressing按钮,继续下一个意图,ProgressDialog [英] Pressing Button and continuing to next Intent, ProgressDialog

查看:122
本文介绍了pressing按钮,继续下一个意图,ProgressDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示一个ProgressDialog时,我的应用程序上传图片。
由于上传图片可能需要相当长一段时间。
现在我有2 Asynctasks,一个上传图片到服务器,以及一个用于发送电子邮件。发送电​​子邮件之后,下一个意图弹出。

I want to show a ProgressDialog when my app is uploading an image. As uploading an image can take quite a while. Now I have 2 Asynctasks, one for uploading an Image to the server, and one for sending the email. After sending the email, the next intent should pop up.

这是code我的下一个按钮:

This is the code for my next Button:

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()

This is my 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

This is my 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秒。

So how can i make it like that the ProgressDialog shows up while uploading (for now it shows up for a few seconds (say 3 seconds) while uploading takes 15 - 20 seconds here. The Dialog should be up for the whole 20 seconds instead of only 3 seconds. Also when the dialog closes, it already goes to the next intent. While it actually should go to the next intent in 20 seconds instead of 3 seconds.

推荐答案

您可以做的是通过步骤4的进度对话框中MyAsyncTask当您使用执行MyAsyncTask 新MyAsyncTask()。执行(_mail)

what you can do is pass your progress dialog of step4 in MyAsyncTask when you execute MyAsyncTask using new MyAsyncTask().execute(_mail)

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);
 }

对不起任何错字。希望这将有助于

sorry for any typo . hope this will help

这篇关于pressing按钮,继续下一个意图,ProgressDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆