簡體   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