AsyncTask中的意图和延迟

时间:2017-05-01 14:07:51

标签: android android-asynctask

我编写用SMTP发送电子邮件的android应用程序。但是当AsyncTask完成工作时,它会立即关闭statusDialog。我想statusDialog延迟3秒,然后转到另一个活动。请帮帮我!

public class SendMailTask extends AsyncTask {

private ProgressDialog statusDialog;
private Activity sendMailActivity;

public SendMailTask(Activity activity) {
    sendMailActivity = activity;

}

protected void onPreExecute() {
    statusDialog = new ProgressDialog(sendMailActivity);
    statusDialog.setMessage("Getting ready...");
    statusDialog.setIndeterminate(false);
    statusDialog.setCancelable(true);
    statusDialog.show();
}

@Override
protected Object doInBackground(Object... args) {
    try {
        Log.i("SendMailTask", "About to instantiate GMail...");
        publishProgress("Processing input....");
        GMail androidEmail = new GMail(args[0].toString(),
                args[1].toString(), (List) args[2], args[3].toString(),
                args[4].toString());
        publishProgress("در حال آماده سازی درخواست...");
        androidEmail.createEmailMessage();
        publishProgress("در حال ثبت درخواست...");
        androidEmail.sendEmail();
        publishProgress("درخواست ثبت شد");
        Log.i("SendMailTask", "Mail Sent.");
    } catch (Exception e) {
        publishProgress(e.getMessage());
        Log.e("SendMailTask", e.getMessage(), e);
    }
    return null;
}

@Override
public void onProgressUpdate(Object... values) {
    statusDialog.setMessage(values[0].toString());

}

@Override
public void onPostExecute(Object result) {
    statusDialog.dismiss();
}

}

2 个答案:

答案 0 :(得分:3)

help fo方法中,为解雇操作添加延迟:

onPostExecute

答案 1 :(得分:0)

试试这个:)

@Override
protected Object doInBackground(Object... args) {
    try {
        Log.i("SendMailTask", "About to instantiate GMail...");
        publishProgress("Processing input....");
        GMail androidEmail = new GMail(args[0].toString(),
                args[1].toString(), (List) args[2], args[3].toString(),
                args[4].toString());
        publishProgress("در حال آماده سازی درخواست...");
        androidEmail.createEmailMessage();
        publishProgress("در حال ثبت درخواست...");
        androidEmail.sendEmail();
        publishProgress("درخواست ثبت شد");
        Log.i("SendMailTask", "Mail Sent.");

Thread.sleep(3000);
    } catch (Exception e) {
        publishProgress(e.getMessage());
        Log.e("SendMailTask", e.getMessage(), e);
    }
    return null;
}