2012-10-05 44 views
1

在Android應用程序時,當我開始活動了顯示黑色屏幕或應用程序掛在幾秒鐘。我想直到黑屏我想顯示進度條。我嘗試了很多次,但無法做到這一點。應用程序掛起不調用Web服務或任何網絡呼叫

String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" 
       + "<category><Id>" + catId + "</Id></category>"; 
StringBuilder resXML = new Connection().getResponseString("http://192.168.1.14/virtualMirror/productlisting.php", xml); // TODO URL change 
if(!resXML.equals("")) { 
    XMLParser parser = new XMLParser(); 
    Document doc = parser.getDomElement(resXML.toString()); // getting DOM element 
    NodeList nodeList = doc.getElementsByTagName("Product"); 

    Intent intent = new Intent(this, ProductListing.class); 
    Bundle bundle = new Bundle(); 
    bundle.putLong("CategoryId", catId); 
    bundle.putString("CategoryName", catName); 
    intent.putExtras(bundle); 
    startActivity(intent); 
} 
+1

使用Aysnctask ..在Google中查找示例。有很多可用 –

+0

看看我的答案。它在我的情況下工作。 –

回答

1

使用的AsyncTask。

的AsyncTask能夠正確且容易使用的UI線程。該類允許執行後臺操作並在UI線程上發佈結果,而無需操縱線程和/或處理程序。

的的AsyncTask另一個線程,它無法訪問到你的意見是GUI內執行doInBackground()一切。

preExecute()postExecute()爲您在此新線程發生繁重事件之前和之後提供GUI訪問權,您甚至可以將長操作的結果傳遞給postExecute()以顯示任何處理結果。

class LoadCategory extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     Pd = new ProgressDialog(getApplicationContext()); 
     Pd.show(); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" 
       + "<category><Id>" + catId + "</Id></category>"; 

     StringBuilder resXML = new Connection().getResponseString("http://192.168.1.14/virtualMirror/productlisting.php",xml); // TODO URL change 
     if (!resXML.equals("")) { 
      XMLParser parser = new XMLParser(); 
      Document doc = parser.getDomElement(resXML.toString()); 
      NodeList nodeList = doc.getElementsByTagName("Product"); 
      return null; 
     } 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     Pd.dismiss(); 
     Intent intent = new Intent(this, ProductListing.class); 
     Bundle bundle = new Bundle(); 
     bundle.putLong("CategoryId", catId); 
     bundle.putString("CategoryName", catName); 
     intent.putExtras(bundle); 
     startActivity(intent); 
    } 
} 

並在您的onCreate()方法中使用此類。

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    new LoadCategory().execute(); 
} 
+0

你不能解釋爲什麼要使用以下? ^^^^ –

+0

對不起@LalitPoptani我編輯我的問題,並感謝記住我.. :) –

+0

歡迎!現在看起來更好:) –

0

您應該先加載帶進度條的屏幕並使用新線程調用此代碼。爲此,您可以創建一個擴展線程類並覆蓋run方法的新類。您將不得不使用這些消息來獲取已加載值的通知。

這是一個快速和骯髒的例子,但希望將足以讓您瞭解整體流程。

private final Handler handler = new Handler(){ 
     public void handleMessage(Message msg) 
     { 
      int total = msg.getData().getInt("total"); 
      if (total <= 0) 
      { 
      //Handle the response here 
      } 
     } 
    }; 

    private class yourclassname extends Thread{ 
     Handler mHandler; 
     String _serviceUrl; 
     CarpoolCancellationLoader(Handler h,String serviceUrl) 
     { 
      mHandler = h; 
      _serviceUrl = serviceUrl; 

     } 

     private class SerializableClassName 
     { 
     ..Put your serializable data here 
     } 

     @Override 
     public void run() 
     { 
      cancelResponse = runJSONParser(); 
      //Send the thread activity done message to the handler 
      Message msg = mHandler.obtainMessage(); 
      Bundle b = new Bundle(); 
      b.putInt("total", -1); 
      msg.setData(b); 
      mHandler.sendMessage(msg); 

     } 

      public YourResponseType runJSONParser() 
      { 
       try 
       { 
       //Perform your loading operation here 
       } 
       catch(Exception ex) 
      { 
       throw ex; 
      } 
      } 

      public String convertStreamToString(InputStream is) 
      { 
       BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
       StringBuilder sb = new StringBuilder(); 

       String line = null; 

       try 
       { 
        while ((line = reader.readLine()) != null) 
        { 
         sb.append(line + "\n"); 
        } 
       } 
       catch (IOException e) 
       { 
        e.printStackTrace(); 
       } 
       finally 
       { 
        try 
        { 
         is.close(); 
        } 
        catch (IOException e) 
        { 
         e.printStackTrace(); 
        } 
       } 

       return sb.toString(); 
      } 
    } 

這不是很乾淨的代碼雖然,但將足以給你的代碼創建新的線程和異步運行它取得成效結構的總體思路。

+0

是的,如果你可以請發送它 –

+0

給我幾分鐘,我給你的代碼 –

+0

感謝您發送... –