简体   繁体   English

Android中AsyncTask中的ProgressDialog

[英]ProgressDialog in AsyncTask in android

I am working on a login in application. 我正在申请中的登录。 I am trying to display progressDialog in my BackgroundWorker class which extends Async task. 我正在尝试在我的BackgroundWorker类中显示progressDialog,以扩展Async任务。 But as soon as I click on Login button, the progress dialog is shown just for a few seconds. 但是,一旦我单击“登录”按钮,进度对话框就会显示几秒钟。 Is there any possibility that signing process will last longer? 签名过程是否有可能持续更长的时间?

Snippets of the code 代码片段

`public class BackgrroundWorker extends AsyncTask<String,Void,String>{

    Context context;
    AlertDialog alertDialog;
    boolean correct;
    public BackgrroundWorker(Context context){
        this.context = context;
    }
    @Override
    protected String doInBackground(String... params) {
        String type = params[0];
        String login_url = "random";
        String register_url = "random";
        if(type.equals("login")){
            try {
                String username = params[1];
                String password = params[2];
                URL url = new URL(login_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String post_data = URLEncoder.encode("username","UTF-8") + "=" +URLEncoder.encode(username,"UTF-8")+ "&"
                    +URLEncoder.encode("password","UTF-8") + "=" +URLEncoder.encode(password,"UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                String result = "";
                String line = "";
                correct = false;
                while((line = bufferedReader.readLine()) != null){
                    result += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                System.out.println(result);
                if (result.equalsIgnoreCase("login success"))
                    correct=true;
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        progressDialog = ProgressDialog.show(context, "Login",
                "Please wait for a while.", true);
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Login status");



    }

    @Override
    protected void onPostExecute(String result) {
        if(!result.equals("login success")) {
            alertDialog.setMessage(result);
            alertDialog.show();

        }
        progressDialog.dismiss();
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
}
`
progressDialog.dismiss();

This statement causing dialog to disappear and you have set it without any condition inside 该语句导致对话框消失,并且您在其中设置了没有任何条件的对话框

onPostExecute

If you want to show dialog for a long time place it inside Handler or CountDownTimer . 如果要长时间显示对话框,请将其放在HandlerCountDownTimer中 You can also make it user interaction based like button click. 您还可以使其成为用户交互的基础,例如单击按钮。 For example, if you want to dismiss dialog for 5 sec: 例如,如果要关闭对话框5秒钟,请执行以下操作:

@Override
protected void onPostExecute(String result) {
    if(!result.equals("login success")) {
        alertDialog.setMessage(result);
        alertDialog.show();

    }
    new CountDownTimer(5000, 1000) {

    public void onTick(long millisUntilFinished) {

    }

    public void onFinish() {
       progressDialog.dismiss();
   }
  }.start();

}

Is there any possibility that signing process will last longer? 签名过程是否有可能持续更长的时间? (signing = signin) (登录=登录)

There sure is. 肯定有。 Imagine the device is being heavily throttled, the server is taking along time to respond, or stuck on a really really slow network (ex: 1g data network, or my local starbucks wifi that 100s for people use at the same time) 想象一下设备正处于严重的节流状态,服务器需要花费一些时间来响应,或者停留在一个非常缓慢的网络上(例如:1g数据网络,或者我的本地星巴克wifi,同时供数百人使用)

You can test this by loading your application on the android emulator and using the emulator console commands for netspeed and netdelay 您可以通过在android模拟器上加载您的应用程序并针对netspeednetdelay使用模拟器控制台命令来netspeed netdelay

The basics steps are something like : 基本步骤如下:

  • telnet localhost 5554
  • network speed 2 1

The first number ("2"). 第一个数字(“ 2”)。 is the upload speed (traffic from the device) in kilobytes per second The second number ("1"), is the download speed (traffic to the device) in kilobytes per second 是上传速度(来自设备的流量),以千字节/秒为单位第二个数字(“ 1”),是下载速度(设备的流量),以千字节/秒为单位

  • network netdelay 10000

Above, we set a 10 second delay/latency of the network 上面,我们设置了10秒的网络延迟/延迟

  • then on the device, perform the login 然后在设备上执行登录

Full documentation to accomplish this is here and I recommend checking them out. 有关完成此操作的完整文档,请参见此处。 Lots of great info : https://developer.android.com/studio/run/emulator-console.html 很多很棒的信息: https : //developer.android.com/studio/run/emulator-console.html

I also found this answer to be helpful: https://stackoverflow.com/a/6236379/794088 我还发现此答案很有帮助: https : //stackoverflow.com/a/6236379/794088

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM