其他分享
首页 > 其他分享> > AsyncTask需要很长时间才能启动Android

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

作者:互联网

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

谢谢

解决方法:

很难通过查看代码来说明为什么它这么慢.我相信,最好的猜测是您正在执行两个Web请求(其中一次正在获取图像).可能是您的服务非常慢.

您在上面发布的日志行表示您正在分配内存.如果您正在做一些占用大量内存的操作,那么可能会看到很多.

也可能有很多其他线程在运行,并且AsyncTask线程的优先级比其他线程低,因此实际上被它们阻塞了.尝试提高其优先级(不知道如何做)或仔细查看代码中的其他线程.

标签:android-asynctask,android
来源: https://codeday.me/bug/20191208/2087870.html