繁体   English   中英

带ProgressDialog的Asynctask

[英]Asynctask w/ ProgressDialog

我知道这有很多答案,但是我找不到我真正需要的东西:

1)用户单击按钮时,显示进度对话框;

2)执行AsyncTask类并等待答案(这是使用HTTPUrlConnection的响应);

3)关闭进度对话框;

我尝试了很多事情,但是进度对话框没有“出现”。 我的代码:

   public class MainActivity extends Activity implements OnTaskCompleted{
    ..
    private ProgressDialog progressDialog;
    private Button btnLogin;
    ..

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            btnLogin = (Button) findViewById(R.id.btnLogin);

            btnLogin.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    progressDialog = ProgressDialog.show(MainActivity.this,
                            "", "Scanning Please Wait", true);
                    try {
                         String param1 = "testParam1";
                         String param2 = "testParam2";
                                String response = new SyncHelper(MainActivity.this).execute("http://server.example.com/api", param1, param2).get(); //this way, my activity waits of the answer
                               Log.d(TAG, "Finished: " + response);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            } catch (ExecutionException e) {
                                e.printStackTrace();
                            }
                        } else {
                            // user didn't entered username or password
                            Toast.makeText(getApplicationContext(),
                                    "Done",
                                    Toast.LENGTH_LONG).show();
                        }
                    } catch (Exception e) {
                    }
                }
            });
        }

        public void onTaskCompleted()
        {
            progressDialog.dismiss();
        }



public class SyncHelper extends AsyncTask<Object, Void, String>
{
..
    private OnTaskCompleted listener;
..
        protected String doInBackground(Object... url) {
            String response = "";
            try {
                response = getRequest((String) url[0],(String) url[1], (String) url[2]); //Here I make a HttpURLConnection
            } catch (IOException e) {
                e.printStackTrace();
            }
            return response;
        }

        @Override
        protected void onPreExecute() {
        }

        protected void onPostExecute(String result) {
            listener.onTaskCompleted();
        }
}
    public interface OnTaskCompleted{
        void onTaskCompleted();
    }
public class MainActivity extends Activity{
    ..

    private Button btnLogin;
    ..

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            btnLogin = (Button) findViewById(R.id.btnLogin);

            btnLogin.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                                       try {
                         String param1 = "testParam1";
                         String param2 = "testParam2";
                                 new SyncHelper(MainActivity.this).execute("http://server.example.com/api", param1, param2); //this way, my activity waits of the answer
                               Log.d(TAG, "Finished: " + response);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            } catch (ExecutionException e) {
                                e.printStackTrace();
                            }
                        } else {
                            // user didn't entered username or password
                            Toast.makeText(getApplicationContext(),
                                    "Done",
                                    Toast.LENGTH_LONG).show();
                        }
                    } catch (Exception e) {
                    }
                }
            });
        }





public class SyncHelper extends AsyncTask<String, Void, String>
{


..

    Context context;
    private ProgressDialog pd;
..

    public SyncHelper (Context c)
    {
         context = c;
    } 


@Override
        protected void onPreExecute() {
            pd = new ProgressDialog(context);
            pd.setTitle("Processing...");
            pd.setMessage("Please wait.");
            pd.setCancelable(false);
            pd.setIndeterminate(true);
            pd.show();
        }
        protected String doInBackground(String... url) {
            String response = "";
            try {
                response = getRequest(url[0], url[1], url[2]); //Here I make a HttpURLConnection
            } catch (IOException e) {
                e.printStackTrace();
            }
            return response;
        }


        protected void onPostExecute(String result) {

            // here you will be getting the response in String result.
                 if (pd.isShowing()) 
                            pd.dismiss();

        }
}

使用get时,使用AsyncTask没有任何意义。 因为get()会阻止UI线程,所以也许这就是为什么看不到进度对话框的原因。 如果要将响应发送回MainActivity ,请像使用beofre一样使用回调接口。

暂无
暂无

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

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