2015-01-20 45 views
-1

所以基本上我只是很快要告诉你的应用程序流,的Android,异步任务的doInBackground期间巨人滞后(实际上是:虽然充气查看)

一个异步任务(一)从获取源代码并将它传递给另一个异步任务(b),其中需要完成几个阵列副本和分割,总共大约500行代码,但正如我所说的,很多if/for/split/arraycopy方法。 。之后,它重新活化viewpager和数据刷新..

所以,当数据被提取和排序的UI应该显示某种加载提示,它做了什么,但约一秒后它完全滞后,整个UI线程是b锁定尽管所有的工作都在doInBackground方法完成..我认为doInBackground方法应该处理“重型” ..

是否有一定的限制还是我失去了一些东西..

我只是张贴我使用的是粗糙的异步任务..

public class ProcessingService extends Service { 

Method1(); 
Method2(); 
Method3(); 
//etc.. about 10 methods with ~500 lines of code.. 

@Override 
public int onStartCommand(Intent iservice1, int flags, int startId) { 
    processingit(); 
    return Service.START_NOT_STICKY; 
} 

public void processingit() { 
    new ProcessingOutsourced().execute(); 
} 

public class ProcessingOutsourced extends AsyncTask<String, Void, Void> { 
    public void onPreExecute() { 

    } 

    public Void doInBackground(String... urls) { 
     Method1(); 
     Method2(); 
     Method3(); 
     //Etc.. 
     return null; 
    } 

    public void onPostExecute(Void unused) { 
     Context b = getBaseContext(); 
     processing goon2 = new processing(b); //Passed onto another class to finish in entirely.. 
     goon2.finishLoading(); 
     stopSelf(); 
    } 
} 

@Override 
public IBinder onBind(Intent intent) { 
    //for communication return IBinder implementation 
    return null; 
} 
} 

所以基本上..我认为我做错了什么在doInBackground一部分。这还是我不允许它传递到另一个类?我本来希望在异步中做所有事情,但我无法在其中使用“viewpager.notifydatasetchanged”..这就是为什么我需要传递它。

任何一种提示都被赞赏..!

编辑:

所以基本上我是一个很懒惰的人,并使用使用占位符片段viewpager片段,我用几个if方法来找出精确地显示哪个页面,从而抬高了新的观点,每次条件变化..

if (getArguments().getInt(ARG_SECTION_NUMBER) == 0) { 
     if (ClassC.rdy == 0 && ClassC.PullFailedAndRefresh == 0) { 
      View rootView11 = inflater.inflate(R.layout.mat_while_loading_in_main, container, false); 
      TextView textAbove = (TextView)rootView11.findViewById(R.id.whileloadingAbove); 
      textAbove.setText("Vertretung Heute wird geladen..."); 
      return rootView11; 
     }else if (ClassC.rdy == 0 && ClassC.PullFailedAndRefresh == 1) { 
      View rootView1111 = inflater.inflate(R.layout.mat_loading_aborted_refresh, container, false); 
      return rootView1111; 
     } else if (ClassC.rdy == 1 && ClassC.abwh.equalsIgnoreCase("Abw") && ClassC.datumh.equalsIgnoreCase("Vertretung Datum eintragen")) { 
      View rootView11111 = inflater.inflate(R.layout.mat_keine_vertretung_eingerichtet, container, false); 
      return rootView11111; 
     } else if (ClassC.rdy == 1 && !ClassC.frageTimeout) { 
      View rootView111 = inflater.inflate(R.layout.mat_loading_aborted, container, false); 
      return rootView111; 
     } else if (ClassC.rdy == 1 && ClassC.pullWorked == 1) { 
      View rootView2 = inflater.inflate(R.layout.mat_vertretungsplan, container, false); 
      TextView textView1 = (TextView) rootView2.findViewById(R.id.section_label); 
      TextView textView121 = (TextView) rootView2.findViewById(R.id.spalte_1); //SPALTE1 
      TextView textView221 = (TextView) rootView2.findViewById(R.id.spalte_2); //SPALTE2 
      TextView textView321 = (TextView) rootView2.findViewById(R.id.spalte_3); //SPALTE3 
      TextView textView421 = (TextView) rootView2.findViewById(R.id.spalte_4); //SPALTE4 
      TextView textView521 = (TextView) rootView2.findViewById(R.id.spalte_5); //SPALTE5 
      TextView textView621 = (TextView) rootView2.findViewById(R.id.spalte_6); //SPALTE6 
      TextView textView721 = (TextView) rootView2.findViewById(R.id.spalte_7); //SPALTE7 
      TextView wat1 = (TextView) rootView2.findViewById(R.id.BonusHinweise); 
      TextView textView31 = (TextView) rootView2.findViewById(R.id.Abwesendenliste); 
      TextView textView41 = (TextView) rootView2.findViewById(R.id.GeneralHinweis); 

      textView1.setTextSize(dgrose); 
      textView1.setText(ClassC.datumh);  //DATUM-H 
      textView121.setTextSize(vpgrose); 
      textView121.setText(Html.fromHtml(ClassC.dieausgabeh1)); //SPALTE1_H 
      textView221.setTextSize(vpgrose); 
      textView221.setText(Html.fromHtml(ClassC.dieausgabeh2)); //SPALTE2_H 
      textView321.setTextSize(vpgrose); 
      textView321.setText(Html.fromHtml(ClassC.dieausgabeh3)); //SPALTE3_H 
      textView421.setTextSize(vpgrose); 
      textView421.setText(Html.fromHtml(ClassC.dieausgabeh4)); //SPALTE4_H 
      textView521.setTextSize(vpgrose); 
      textView521.setText(Html.fromHtml(ClassC.dieausgabeh5)); //SPALTE5_H 
      textView621.setTextSize(vpgrose); 
      textView621.setText(Html.fromHtml(ClassC.dieausgabeh6)); //SPALTE6_H 
      textView721.setTextSize(vpgrose); 
      textView721.setText(Html.fromHtml(ClassC.dieausgabeh7)); //SPALTE7_H 
      textView31.setTextSize(agrose); 
      textView31.setText(/*"\n" + */ClassC.abwh);  
      textView41.setTextSize(hgrose); 
      textView41.setText(R.string.atung);  

      if (ClassC.watchh != 0) { 
       wat1.setTextSize(hgrose); 
       wat1.setText(""); 
      } else if (ClassC.abwfuckedup != 0) { 
       wat1.setTextSize(hgrose); 
       wat1.setText(""); 
      } else if (ClassC.watchh != 0 && ClassC.abwfuckedup != 0) { 
       wat1.setTextSize(hgrose); 
       wat1.setText(""); 
      } else { 
       wat1.setTextSize(hgrose); 
       wat1.setText("Kein Hinweis"); 
      } 
      return rootView2; 

     } 

这仅仅是4个不同状态的页面之一,好像这是太多。我应该为每个页面创建一个单独的类吗?除了阅读android文档之外,还有什么可以帮助你:?

+0

即时消息肯定滞后不会发生在doInBackground和它在onPostExecute中做的任何事情。你是否尝试设置断点并查看滞后发生的位置? – tyczj 2015-01-20 22:00:17

+0

1)不要使用'getBaseContext()',使用'this'。 2)什么是类“处理”? – 2015-01-20 22:11:23

+0

与突破点的想法使我发现,滞后是由几个意见膨胀引起的(4)似乎太多..我将编辑的主要职位,当我回家 – 1sm3t 2015-01-21 06:20:33

回答

2
public void onPostExecute(Void unused) { 
    Context b = getBaseContext(); 
    processing goon2 = new processing(b); //Passed onto another class to finish in entirely.. 
    goon2.finishLoading(); 
    stopSelf(); 
} 

此代码是在UI线程上运行 - 只有doInBackground()在一个单独的线程运行,因为它的预期,你会用它来与新的数据来更新你的界面onPostExecute()不你的任务已被提取/制作。

因此,您在此处运行的任何冗长操作都会阻止UI线程,这会导致您看到的效果。

相关问题