简体   繁体   English

调用新的MyAsyncTask()。execute()。get()Android时进度对话框不起作用

[英]progress dialog is not working while calling new MyAsyncTask().execute().get() Android

how to show progress dialog on onPreExecute() of AsyncTask android below is my code here in this onPreExecute() method progress dialog is not showing on UI Thread 如何在AsyncTask android的onPreExecute()上显示进度对话框,下面是我的代码,此onPreExecute()方法中的进度对话框未显示在UI线程上

//calling AsyncTask //调用AsyncTask

try {

        new XYZ().execute(employeeId, password).get();
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (ExecutionException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }


     class XYZ extends AsyncTask<String, Integer, Void> {

    ProgressDialog simpleWaitDialog;

    @Override
    protected void onPreExecute() {


        simpleWaitDialog =  new ProgressDialog(TreeViewListDemo.this);
        System.out.println("fgfdgfgfgf"+simpleWaitDialog);
        simpleWaitDialog.setMessage("Loading...");
        simpleWaitDialog.setIndeterminate(true);
        simpleWaitDialog.setCancelable(true);


        simpleWaitDialog.show();

    }

    @SuppressWarnings("unchecked")
    @Override
    protected Void doInBackground(String... params) {

        String username = params[0];
        String password = params[1];
        // this is code for perfoming SOAP web services which is working    fine and getting data from server side
        return null;
    }

}

You are calling get() , presumably from the UI thread. 您正在调用get() ,大概是从UI线程调用的。 From the documentation, the get() method: 从文档中, get()方法:

Waits if necessary for the computation to complete, and then retrieves its result. 必要时等待计算完成,然后检索其结果。

This means you block the UI thread so that it cannot show the progress dialog. 这意味着您将阻止UI线程,使其无法显示进度对话框。 I think you want to replace the call to get() with execute() 我认为您想将对get()的调用替换为execute()

Progres dialog show untill the doInBackground method got execute. Progres对话框显示直到执行doInBackground方法。

And Here you are just Run below two line 在这里,您只是在两行以下运行

String username = params[0];
String password = params[1];

Is hardly take time in millisecond so your progress show and close in millisecond. 几乎不需要花费毫秒的时间,因此您的进度会以毫秒显示并关闭。 Please Put log values in onPreExecute() to check your progress dialog show our not. 请把日志值放在onPreExecute()以检查进度对话框,显示不是。

AsyncTask is normally used to perform Network operations..!!

HapPy coading..!! HapPy涂胶.. !!

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

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