2013-01-18 38 views

回答

5
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(); 
+0

onProcessupdate方法如何針鋒相對嘗試捕捉博客傳遞兩個字符串值。 – Yugesh

+1

從edittexts獲得用戶名和密碼後,您可以直接使用它們 –

+0

thanks.then如果沒有互聯網連接意味着進度條加載不解僱。 – Yugesh

0

使用的AsyncTask在你的代碼,並把你的代碼在doInBackground(....)過程。

在onPreExecute中顯示您的進度對話框並在onPostExecute(...)中關閉它。

0

爲您的佈局添加無限進度欄視圖,並使其首先隱藏。

創建一個AyncTask來進行服務器通信。

onPreExecute()使進度欄可見。

onPostExecute()中再次隱藏進度條。

0

可以使用的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(); 

     } 
    } 
相關問題