1

在 android 应用程序中,当我启动活动时,它显示黑屏或应用程序会挂起几秒钟。我想直到黑屏我想显示进度条。我尝试了很多次,但无法做到这一点。

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);
}
4

2 回答 2

1

使用异步任务。

AsyncTask 允许正确和轻松地使用 UI 线程。此类允许在 UI 线程上执行后台操作并发布结果,而无需操作线程和/或处理程序。

AsyncTask 在doInBackground()另一个线程内执行所有内容,该线程无权访问您的视图所在的 GUI。

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();
}
于 2012-10-05T11:49:14.587 回答
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();
          }
    }

虽然这不是很干净的代码,但足以让您全面了解创建新线程并通过异步运行它获得结果的代码结构。

于 2012-10-05T11:48:27.613 回答