AsyncTask需要很长时间才能启动Android

时间:2011-09-07 08:44:05

标签: android android-asynctask

AM使用AsyncTask从服务中检索数据。但它耗费了太多时间,即大约2分钟左右。

我发现,当我启动asynctask时,对话框显示但是后台进程需要时间来响应和获取数据。

检查我的asynctask代码:

 public class JamsTask extends AsyncTask<Void,Void,Bundle> 
     { 


            private Context ctx; 
            ProgressDialog dlg;
            Bitmap userImage;
            String message ;

            public JamsTask(Context context) { 
                ctx = context; 
            } 

            @Override 
            protected void onPreExecute() { 
                //super.onPreExecute(); 
           try{
                dlg = new ProgressDialog(Jams.this); 
                dlg.setMessage("Please wait...."); 
                dlg.show();
           }catch (Exception e) {
            // TODO: handle exception
        }


                //setContentView(R.layout.splash);
            } 


            @Override
            protected Bundle doInBackground(Void... params) {
                   Bundle b=new Bundle();



                try{

                 SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);



                 request.addProperty("fkPersonId", Integer.valueOf(JujamaMain.userValues.get(0)));
                 request.addProperty("fkConferenceId", Integer.valueOf(JujamaMain.userValues.get(4)));
                 request.addProperty("startIndex",1);
                 request.addProperty("endIndex",20);



                   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                   envelope.dotNet=true;
                   envelope.setOutputSoapObject(request);

                   HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                   androidHttpTransport.call(SOAP_ACTION, envelope);

                   Object result= envelope.getResponse(); 

                   SoapObject response=(SoapObject)envelope.bodyIn;



                  strRes = result.toString();





                  SoapObject returnObj = (SoapObject)response.getProperty("GetAllStatusUpdatesResult");

                  SoapObject object=(SoapObject) returnObj.getProperty(1);


                  SoapObject returnObj2 = (SoapObject)object.getProperty("NewDataSet");



                 // Integer id = Integer.valueOf(returnObj.getPropertySafelyAsString("id");


                  for(int i=0;i<returnObj2.getPropertyCount();i++) {
                      SoapObject persondetails = (SoapObject)returnObj2.getProperty(i);



                      String salution = (String)persondetails.getPropertySafelyAsString("Salutation","");
                      //String companyname = (String)persondetails.getPropertySafelyAsString("CompanyName");
                      String firstname = (String)persondetails.getPropertySafelyAsString("FirstName","");

                      String lastname = (String)persondetails.getPropertySafelyAsString("LastName","");



                     if(!persondetails.getPropertySafelyAsString("Message","").contains("anyType"))
                        message = (String)persondetails.getPropertySafelyAsString("Message","");
                     else
                         message=""
;                     
                      String nameID = (String)persondetails.getPropertySafelyAsString("PKStatusID","");




                      String fkconfid = (String)persondetails.getPropertySafelyAsString("FKConferenceID","");





                  try{   

                     SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                      java.util.Date date = null;
                      String sub=commented.substring(0, 19);

                       date = form.parse(sub);


                      SimpleDateFormat postFormater =new SimpleDateFormat("MMMMM dd, yyyy, HH:mm");
                      String newDateStr = postFormater.format(date);

                      mTimedifference.add(newDateStr);


                   /* DateTime myBirthDate = new DateTime(year,month, day, hour, minute, 0, 0);
                      DateTime now = new DateTime();
                      Period period = new Period(myBirthDate, now);

                      PeriodFormatter formatter = new PeriodFormatterBuilder()
                          .appendYears().appendSuffix(" years, ")
                          .appendMonths().appendSuffix(" months, ")
                          .appendWeeks().appendSuffix(" weeks, ")
                          .appendDays().appendSuffix(" days, ")
                          .appendHours().appendSuffix(" hours, ")
                          .appendMinutes().appendSuffix(" minutes, ")

                          .printZeroNever()
                          .toFormatter();

                      String elapsed = formatter.print(period);
                      System.out.println(elapsed + " ago");*/

                  }catch (Exception e) {
                    // TODO: handle exception
                      e.printStackTrace();

                }

                     // System.out.println(".."+"dat"+now+"..."+time);
                      mNameID.add(nameID);


                      String ImageUrl = (String)persondetails.getPropertySafelyAsString("PhotoGuid","");

                      String liked = (String)persondetails.getPropertySafelyAsString("Liked","No");
                      String nocomments = (String)persondetails.getPropertySafelyAsString("NoofComments","0");

                      mName.add(firstname.toString()+lastname.toString()+salution.toString());
                      mMessages.add(message.toString());
                      mLikesCount.add(likescount.toString());



                         mLikeText.add(liked);
                         mNoComments.add(nocomments);




                         userImage=getBitmapFromURL("http://test.jujama.com/uploadfiles/"+ImageUrl);

                      Bitmap bmp=BitmapFactory.decodeResource(getResources(),R.drawable.nophoto);

                      if(userImage==null)
                             mUserImage.add(bmp);
                         else
                          mUserImage.add(userImage);


                  }




                } catch (Exception e) {
                    e.printStackTrace();

                }

                return b;
            }    





              @Override 
                protected void onPostExecute(Bundle b) { 

                 dlg.dismiss(); 

                 lv.setAdapter(new Viewadapter(Jams.this));





              }

         } 

有没有办法减少时间。因为它的时间太长了。

还有一个:

开始之前:以下类型的线路即将到来。

 GC_FOR_MALLOC freed 31921 objects / 2006888 bytes in 100ms

由于

1 个答案:

答案 0 :(得分:3)

很难说为什么从你的代码看它为什么这么慢。最好的猜测是,我相信你做两个网络请求(其中一个是获取图像)。可能你的服务很慢。

您在上面发布的日志行意味着您正在分配内存。如果你正在做一些记忆密集的事情,你可以期待看到很多内容。

也有可能你有很多其他线程,并且AsyncTask线程的优先级低于其他线程,然后基本上被它们阻止。尝试提高其优先级(不知道该怎么做)或仔细查看代码中的其他线程。