可能重複:
Android AsyncTask Progress bar如何設置進度條,直到從服務器使用json獲取數據?
現在用的登錄過程,在針鋒相對的時間正在逐漸從服務器上的數據是delay.so我想設置達delay.how到進度條設置進度條,直到得到服務器的響應。任何知道答案請幫助我。
可能重複:
Android AsyncTask Progress bar如何設置進度條,直到從服務器使用json獲取數據?
現在用的登錄過程,在針鋒相對的時間正在逐漸從服務器上的數據是delay.so我想設置達delay.how到進度條設置進度條,直到得到服務器的響應。任何知道答案請幫助我。
private class LongOperation extends AsyncTask<String, Void, String>
{
protected void onPreExecute()
{
progressDialog = new ProgressDialog(activity.this);
progressDialog.setTitle("Processing...");
progressDialog.setMessage("Please wait...");
progressDialog.setCancelable(true);
progressDialog.show();
}
protected String doInBackground(String... params)
{
try
{
//Getting data from server
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result)
{
progressDialog.dismiss();
Intent n = new Intent(firstactivity.this, secondactivity.class);
startActivity(n);
}
}
如何調用該
ProgressDialog progressDialog;
LongOperation mytask = null;
mytask = new LongOperation();
mytask.execute();
使用的AsyncTask在你的代碼,並把你的代碼在doInBackground(....)過程。
在onPreExecute中顯示您的進度對話框並在onPostExecute(...)中關閉它。
爲您的佈局添加無限進度欄視圖,並使其首先隱藏。
創建一個AyncTask
來進行服務器通信。
在onPreExecute()
使進度欄可見。
在onPostExecute()
中再次隱藏進度條。
可以使用的AsyncTask
private class GetLogin extends AsyncTask<String, Integer, String> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show("Downloading...");
}
@Override
protected String doInBackground(String... params) {
for (String myUrl : params) {
try {
URL url = new URL(myUrl);
URLConnection ucon = url.openConnection();
ucon.setRequestProperty("Accept", "application/xml");
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
String str = new String(baf.toByteArray(), "UTF8");
return str;
} catch (MalformedURLException e) {
//error
} catch (IOException e) {
//error
}
}
return "All Done!";
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
pd.setMessage("Downloading... (" + values[0] + "%)");
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.dismiss();
}
}
的
onProcessupdate
方法如何針鋒相對嘗試捕捉博客傳遞兩個字符串值。 – Yugesh從edittexts獲得用戶名和密碼後,您可以直接使用它們 –
thanks.then如果沒有互聯網連接意味着進度條加載不解僱。 – Yugesh