Search code examples
javaandroidmultithreadingthread-sleep

Android "Skipped XX frames!"


So I am learning on how to develop android applications. I am trying to get this program to flash some letters, one at a time fairly quickly on a textView, but when I try this on my android device and it does not work I get the message "I/Choreographer﹕ Skipped 57 frames! The application may be doing too much work on its main thread." (AttetionalBlinkTrial is a class that has a field called "blinkList" that is an ArrayList of strings)

public void startTask(View view) throws InterruptedException {
    TextView textView = (TextView) findViewById(R.id.display);
    AttentionalBlinkTrial theTrial = new AttentionalBlinkTrial();
    theTrial.generateTargets();
    theTrial.generateBlinkList(5);
    for (int i = 0; i <= 5; i++) {
        textView.setText(theTrial.getBlinkList().get(i));
        Thread.sleep(40);
        textView.setText(");
        Thread.sleep(40);
    }
}

Solution

  • Thread.sleep makes UI thread inaccessible. You should use Handler class instead. Sorry I can't provide any codes since I am on mobile but it's quite easy. If i remember right "postDelayed" method is what you need.

    public void blink(TextView textView) {
        if (textView.getText() == "Blink!") {
            textView.setText("");
        } else {
            textView.setText("Blink!");
        }
    }
    
    public void blinkingTask() throws InterruptedException {
        final Handler handler = new Handler();
        final TextView textView = (TextView) findViewById(R.id.my_text);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                blink(textView);
            }
        };
    
        for (int i = 0; i <= 5; i++) {
            handler.postDelayed(runnable, 1000 + (i * 1000)); // 5 delayed actions with 1000 ms interval.
        }
    }