【问题标题】:ProgressDialog in AsyncTask in androidandroid中AsyncTask中的ProgressDialog
【发布时间】:2018-01-28 13:47:33
【问题描述】:

我正在登录应用程序。我正在尝试在扩展异步任务的 BackgroundWorker 类中显示 progressDialog。但是只要我点击登录按钮,进度对话框就会显示几秒钟。签名过程是否有可能持续更长时间?

代码片段

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

【问题讨论】:

    标签: java android android-asynctask progressdialog


    【解决方案1】:
    progressDialog.dismiss();
    

    此语句导致对话框消失,并且您在内部没有任何条件地设置它

    onPostExecute
    

    如果您想长时间显示对话框,请将其放在Handler CountDownTimer中。您还可以使其基于用户交互,例如按钮单击。 例如,如果你想关闭对话框 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();
    
    }
    

    【讨论】:

      【解决方案2】:

      签名过程是否有可能持续更长时间? (签名 = 登录)

      确实有。想象一下设备被严重限制,服务器需要时间来响应,或者卡在一个非常慢的网络上(例如:1g 数据网络,或者我的本地星巴克 wifi,100 多个人同时使用)

      您可以通过在 android 模拟器上加载您的应用程序并使用模拟器控制台命令 netspeednetdelay 来测试这一点

      基本步骤如下:

      • telnet localhost 5554
      • network speed 2 1

      第一个数字(“2”)。是以千字节每秒为单位的上传速度(来自设备的流量) 第二个数字(“1”)是下载速度(到设备的流量),以千字节每秒为单位

      • network netdelay 10000

      在上面,我们设置了 10 秒的网络延迟/延迟

      • 然后在设备上执行登录

      完成此操作的完整文档在这里,我建议您查看它们。很多很棒的信息:https://developer.android.com/studio/run/emulator-console.html

      我还发现这个答案很有帮助: https://stackoverflow.com/a/6236379/794088

      【讨论】:

        猜你喜欢
        • 2014-12-18
        • 1970-01-01
        • 1970-01-01
        • 2015-04-15
        • 2015-05-12
        • 1970-01-01
        • 1970-01-01
        • 2017-10-24
        相关资源
        最近更新 更多