调用意图后,启动活动需要很长时间,并且应用程序是动态应用程序。我正在连接到服务器并从服务器检索信息。我希望立即开始活动,但我尝试了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);
}
}
}
}
可以给我一些建议。