连续延迟的android处理程序

时间:2013-10-20 03:39:46

标签: android handler postdelayed

我正在编写一个应用程序来读取一行文本,在读取行时连续突出显示每个单词。我的想法是开始播放这一行(这是一本适合孩子的图画书,一次一行),然后阅读文本,每个单词的长度以毫秒为单位,然后在正确的时间突出显示文本视图中的单词。 / p>

我的方法是: 把句子的单词放到一个数组中(最终每个作品的长度,但目前只假设1000ms); 将单词写入textViewPartial; 延迟字长; 将下一个单词添加到句子并将其写入textViewPartial .... etc。

但我无法确定时机。阅读我在处理程序和异步上可以找到的所有内容,我能想到的最好的内容如下 - 我在for循环中放置了一个postdelayed处理程序。我的大脑说每次循环都是looper时它会延迟,但你可以从logcat输出看到它没有。在for循环开始之前只有一个延迟。这是我的第一篇文章,我看不出你们是如何从Eclipse中获取彩色代码的,所以我希望它看起来不错。

   public class LineOutHanler extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line_out_hanler);
    TextView t = new TextView(this);
    t=(TextView) findViewById (R.id.textView10);
    String textOut = "Oh how my spleen aches to see you again";
    final TextView textViewPartial = (TextView) findViewById(R.id.textView11);
    final String[] wordsOut = textOut.split(" ");
    final int wordsInSentence = wordsOut.length;
    int[] wordLength = new int[wordsInSentence];
            for (int counter=0;counter<=wordsInSentence-1;counter++){
            wordLength[counter]=wordsOut[counter].length();}
    String partialSentence ="";
    for (int counter=0; counter<=wordsInSentence-1; counter++){   
            String  c= addWordsOut(wordsOut[counter], partialSentence);
            textViewPartial.setText(c);
            partialSentence = c;
            Log.d("Word", partialSentence);
    final String partialOut=c;
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
                        textViewPartial.setText(partialOut);
            Log.d("Handler", partialOut);
        }
    } , 2000);}
    }
public String addWordsOut (String part, String upToHere)  {
upToHere=upToHere+" " + part;
return upToHere;
}
}

和logcat输出:

10-19 23:07:32.248: E/cutils-trace(39): Error opening trace file: No such file or directory (2)
10-19 23:07:32.368: D/AudioSink(39): bufferCount (4) is too small and increased to 12
10-19 23:07:32.379: D/Word(821):  Oh
10-19 23:07:32.379: D/Word(821):  Oh how
10-19 23:07:32.388: D/Word(821):  Oh how my
10-19 23:07:32.388: D/Word(821):  Oh how my spleen
10-19 23:07:32.388: D/Word(821):  Oh how my spleen aches
10-19 23:07:32.388: D/Word(821):  Oh how my spleen aches to
10-19 23:07:32.388: D/Word(821):  Oh how my spleen aches to see
10-19 23:07:32.398: D/Word(821):  Oh how my spleen aches to see you
10-19 23:07:32.398: D/Word(821):  Oh how my spleen aches to see you again
10-19 23:07:33.328: I/Choreographer(288): Skipped 30 frames!  The application may be doing too much work on its main thread.
10-19 23:07:33.368: I/ActivityManager(288): Displayed com.example.testtextout/.LineOutHanler: +1s820ms
10-19 23:07:35.320: W/AudioFlinger(39): write blocked for 1091 msecs, 1 delayed writes, thread 0x40e0b008
10-19 23:07:35.320: D/Handler(821):  Oh
10-19 23:07:35.329: D/Handler(821):  Oh how
10-19 23:07:35.329: D/Handler(821):  Oh how my
10-19 23:07:35.329: D/Handler(821):  Oh how my spleen
10-19 23:07:35.329: D/Handler(821):  Oh how my spleen aches
10-19 23:07:35.329: D/Handler(821):  Oh how my spleen aches to
10-19 23:07:35.339: D/Handler(821):  Oh how my spleen aches to see
10-19 23:07:35.339: D/Handler(821):  Oh how my spleen aches to see you
10-19 23:07:35.339: D/Handler(821):  Oh how my spleen aches to see you again
10-19 23:08:30.588: D/dalvikvm(396): GC_FOR_ALLOC freed 452K, 15% free 3047K/3556K, paused 40ms, total 65ms
10-19 23:25:42.149: D/dalvikvm(288): GC_FOR_ALLOC freed 850K, 31% free 5593K/7996K, paused 99ms, total 117ms

第一个问题 - 这首先是正确的方法吗? 第二个问题 - 我怎样才能让它发挥作用?

非常感谢。

1 个答案:

答案 0 :(得分:0)

问题出在你的for循环中。当您发布runnable时,您总是会在当前时间点发布以运行2000ms。当您的代码运行时,它几乎同时发布这些操作。因此,您会看到输出在2秒后发生,同时发生。相反,你可以做我在下面做的事情,根据你正在处理的单词,我将来会发布2000ms的倍数。

for (int counter=0; counter<=wordsInSentence-1; counter++){   
        String  c= addWordsOut(wordsOut[counter], partialSentence);
        textViewPartial.setText(c);
        partialSentence = c;
        Log.d("Word", partialSentence);
        final String partialOut=c;
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                        textViewPartial.setText(partialOut);
                        Log.d("Handler", partialOut);
               }
        } , 2000*(counter+1));
}

至于你的实现,我建议发布每个新的runnable,因为前一个完成。否则,你可以创建和发布许多runnables,不必要地吞噬你的内存使用,并使处理程序的清理成为一种痛苦。对于初始POC,这不是太糟糕,可以在以后轻松更改。