0

调用意图后,启动活动需要很长时间,并且应用程序是动态应用程序。我正在连接到服务器并从服务器检索信息。我希望立即开始活动,但我尝试了AsyncTask但没有任何结果。

MainActivity.java

public class MainActivity extends Activity {
    private ArrayList<Data> arraydata;
    private Data data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Handler handler=new Handler();
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                //finish();
                new ShowDialogAsyncTask().execute();
                /*Intent i=new Intent(MainActivity.this,XMLParsingActivity.class);
                startActivity(i);*/
            }

        },3000);
    }
    private class ShowDialogAsyncTask extends AsyncTask<Void, Integer, Void>{

       int progress_status;
         TextView txt_percentage=new TextView(MainActivity.this);
        protected void onPreExecute() {
               // update the UI immediately after the task is executed
               super.onPreExecute();
               Toast.makeText(MainActivity.this, "Loading.....", Toast.LENGTH_LONG).show();

              //  Intent i=new Intent(MainActivity.this,XMLParsingActivity.class);
                //MainActivity.this.startActivity(i);

                progress_status = 0;
                txt_percentage.setText("downloading 0%");

              }
    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        Intent i=new Intent(MainActivity.this,XMLParsingActivity.class);
        startActivity(i);

          /* while(progress_status<100){

                progress_status += 5;

                publishProgress(progress_status);
                SystemClock.sleep(200);

               }*/
           //Intent i=new Intent(MainActivity.this,XMLParsingActivity.class);
            //startActivity(i);
return null;

    }
    @Override
      protected void onProgressUpdate(Integer... values) {
       super.onProgressUpdate(values);
        ProgressBar progressBar=new ProgressBar(MainActivity.this);
       progressBar.setProgress(values[0]);
       txt_percentage.setText("downloading " +values[0]+"%");

      }
    @Override
      protected void onPostExecute(Void result) {
       super.onPostExecute(result);
      /* Toast.makeText(MainActivity.this, "Loading.....", Toast.LENGTH_LONG).show();
        txt_percentage.setText("download complete");
        Intent i=new Intent(MainActivity.this,XMLParsingActivity.class);
        MainActivity.this.startActivity(i);*/

      }

    }

}

从我想移动到的主要活动中XMLParsingActivity,它由ListViews从服务器检索到的 xml 数据组成。在 XMLParsingActivity 我使用了动态信息。

XMLParsingActivity.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        //mCustomView = getLayoutInflater().inflate(R.layout.home, null);

        //final ActionBar bar = getActionBar();
        //bar.setCustomView(mCustomView, new ActionBar.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        //arraydata=getIntent().getParcelableExtra("ary");
        listview=(ListView)findViewById(R.id.lv);
        arraydata =new ArrayList<Data>();
         Uri="http://vegaentertain.com/android/list.xml";
          // Toast.makeText(this, "Loading List.....", Toast.LENGTH_LONG).show();

        new ShowDialogAsyncTask().execute();
}

    private class ShowDialogAsyncTask extends AsyncTask<Void, Integer, Void>{

           int progress_status;
            protected void onPreExecute() {
                   // update the UI immediately after the task is executed
                   super.onPreExecute();
                  // ProgressBar bar=new ProgressBar(XMLParsingActivity.this);
                 //  bar.setVisibility(View.GONE);
                  Toast.makeText(XMLParsingActivity.this, "Loading List.....", Toast.LENGTH_LONG).show();
                  }
        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub
             XMLParser parser=new XMLParser();

                String xml=parser.getXmlFromUri(Uri);
                Document doc=parser.getDomElement(xml);
                NodeList nodes=doc.getElementsByTagName("actress");

                for(int i=0;i<nodes.getLength();i++){
                    data=new Data();
                    Element e=(Element)nodes.item(i);
                    data.setName(parser.getValue(e,"name"));
                    data.setDate(parser.getValue(e,"date"));
                    data.setImageurl(parser.getValue(e,"image-url"));
                    arraydata.add(data);
                }


    return null;

        }
        @Override
          protected void onProgressUpdate(Integer... values) {
           super.onProgressUpdate(values);



          }
        @Override
          protected void onPostExecute(Void result) {
           super.onPostExecute(result);
            listview.setAdapter(new CustomAdapter(arraydata,XMLParsingActivity.this));
            listview.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                    // TODO Auto-generated method stub
                    new ShowAsyncTask(arg2).execute();

                }
            });

          }
        private class ShowAsyncTask extends AsyncTask<Void, Integer, Void>{
            int pos;
            public ShowAsyncTask(int arg2) {
                // TODO Auto-generated constructor stub
                pos=arg2;
            }
            protected void onPreExecute() {
                   // update the UI immediately after the task is executed
                   super.onPreExecute();

                     connectionProgressDialog = new ProgressDialog(XMLParsingActivity.this);
                    connectionProgressDialog.setCancelable(true);
                    connectionProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                    connectionProgressDialog.setMessage("Connecting to Server...");
                    connectionProgressDialog.show();
                    }

            @Override
            protected Void doInBackground(Void... params) {
                // TODO Auto-generated method stub

                return null;
            }
             protected void onProgressUpdate(Integer... values) {
                   super.onProgressUpdate(values);
                    connectionProgressDialog.dismiss();
                   // downloadSpinnerProgressDialog.show();
                }
            @Override
            protected void onPostExecute(Void result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);
                Intent i=new Intent(XMLParsingActivity.this,List.class);
                i.putExtra("pos",pos);
                XMLParsingActivity.this.startActivity(i);
            }

        }
        }

        }

可以给我一些建议。

4

1 回答 1

2
  1. MainActivity.java 中不需要 AsyncTask。(注意:你不能从 AsyncTask() 的 doInbackground 调用意图)。通过查看您的代码,我相信它只是一个闪屏。在这种情况下,请使用以下内容:

    @Override
    protected void onStart() {
    
    super.onStart();
    
     Handler handler = new Handler();
    
        // run a thread after 2 seconds to start the home screen
        handler.postDelayed(new Runnable() {
    
            @Override
            public void run() {
    
                   //code written here will execute after 2000ms(2 seconds)
                   // write your intent to move from MainActivity.this to XMLParsingActivity.class
    
        }, 2000); 
      }
    }
    
  2. 这需要时间,因为您调用了“new ShowDialogAsyncTask().execute();” 在 XMLParsingActivity 类的 onCreate() 方法中。由于它需要向服务器发送请求、获取响应、解析和更新 UI,因此显然需要时间。

  3. 您可以做的是,在 onStart() 方法中调用该 AsyncTask 。最初显示一个 ProgressDialog 以让用户知道某事正在进行中。最后在 onPostExecute() 中,只需将 ProgressBar 的可见性设置为 GONE 并使内容的可见性可见。

于 2013-04-02T09:26:35.953 回答