如何增加android应用程序的响应时间?

时间:2012-07-14 09:01:38

标签: android listview

我正在开发一个Android应用程序,当应用程序从Web服务获取某些数据时,它会显示一段空白屏幕。我怎样才能防止这种情况发生?我很感激你的帮助。

    protected void onListItemClick(ListView l, View v, final int position,
        long id) {
    super.onListItemClick(l, v, position, id);

    progressDialog = ProgressDialog.show(ProjectListActivity.this,
            "Please wait...", "Loading...");

    new Thread() {

        public void run() {

            try {
                String project = titles.get(position - 1);

                performBackgroundProcess(project);

            } catch (Exception e) {

                Log.e("tag", e.getMessage());

            }

            progressDialog.dismiss();
        }

    }.start();





private void performBackgroundProcess(String project) {

    String spaceId = null;
    String spaceName = null;
    /*
     * for (Space space : spaces){
     * if(space.getName().equalsIgnoreCase((String) ((TextView)
     * v).getText())){ spaceId = space.getId(); } }
     */
    for (Space space : spaces) {

        if (project.equals(space.getName())) {

            newSpace = space;
        }

    }

    spaceId = newSpace.getId();
    spaceName = newSpace.getName();

    /*
     * Intent intent = new Intent(this, SpaceComponentsActivity.class);
     * intent.putExtra("spaceId", spaceId); intent.putExtra("tabId", 0);
     * intent.putExtra("className", "TicketListActivity"); TabSettings ts =
     * new TabSettings(); ts.setSelTab(1); this.startActivity(intent);
     */
    Intent intent = new Intent(this, SpaceComponentsActivity.class);
    intent.putExtra("spaceId", spaceId);
    intent.putExtra("tabId", 0);
    intent.putExtra("spaceName", spaceName);

    // intent.putExtra("className", "TicketListActivity");
    TabSettings ts = new TabSettings();
    ts.setSelTab(0);
    ts.setSelTabClass("TicketListActivity");
    this.startActivity(intent);

1 个答案:

答案 0 :(得分:1)

这意味着您正在UI线程上运行与网络相关的操作。您应该考虑使用AsyncTask<?, ?, ?>来运行网络线程中的操作以防止UI被锁定。

示例:

@Override
public void onResume() {
    super.onResume();
    new MyAsyncTask().execute();
}

private class MyAsyncTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {

         // Do your network operations here

    }

    @Override
    protected void onPostExecute(Void result) {

       // Add items to your ListView here


    }

}