2012-10-05 23 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

感谢您发送... –