调用intent后需要很长时间才能启动活动,应用程序是一个动态应用程序。我正在连接到服务器并从服务器检索信息。我希望立即启动活动,并尝试使用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);
}
}
}
}
有些人可以给我一些建议。
答案 0 :(得分:2)
MainActivity.java中不需要AsyncTask。 (注意:你不能从AsyncTask()的doInbackground调用intent。看到你的代码我相信它只是一个闪屏。在这种情况下使用以下内容:
@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);
}
}
这需要时间,因为你已经调用了“new ShowDialogAsyncTask()。execute();”在XMLParsingActivity类的onCreate()方法中。由于它需要向服务器发送请求,获取响应,解析和更新UI显然需要时间。
你可以做的是,在onStart()方法中调用AsyncTask。最初显示ProgressDialog以让用户知道某些内容正在进行中。最后在onPostExecute()中,只需将ProgressBar的可见性设置为GONE,并使内容的可见性可见。