0

On Android Developer Site has said that : We shouldn't use long operations work in UIThread. Instead, we should do it on another thread. At this site, they still say :

if the UI thread is blocked for more than a few seconds (about 5 seconds currently) the user is presented with the infamous "application not responding" (ANR) dialog.

To test this statement, I make a proof. Here is my code:

package com.app;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Example extends Activity{
    TextView  textView;
    @Override 
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        textView = new TextView(this);
        textView.setText("please wait");
        setContentView(textView);

    }

    @Override
    public void onResume(){
        super.onResume();
        try {
            Thread.sleep(8000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        textView.setText("end waiting !!!");
    }

}

As you see, I make current thread (UIThread) sleep 8 seconds. So, I think, if I run this apps, Android will notice: "application not responding". But, I don't see any error. Program run smoothly (and I see text "end waiting", too).

So,who can explain for me about my code. What wrong thing I have made,please.

thanks :)

2 Answers 2

8

The problem here is that you don't generate any signal/event that gets blocked. Android displays that dialog when you or the system doesn't respond to an event within these 5 seconds. No event means no dialog.

So your code runs fine until you try to do something. If I run your code without doing anything, it works without issues. If I generate an event by pressing the menu-key at the start it will trigger an ANR because the key event could not be processed properly.

Hope this help :)

3
  • Good answer. If the OP intends to do anything that will block the UI Thread for more than 5 seconds it should go into an AsyncTask ideally.
    – Knossos
    Feb 25, 2012 at 17:50
  • 2
    @alextsc Can you give me a detailed example, please. I have create a new button and make it an event onRefresh. After that, when I start apps, I press that Button, but still no problem happended. Here my code for refresh:
    – hqt
    Feb 26, 2012 at 3:09
  • 2
    public void onRefresh(View view){ try { Thread.sleep(8000); } catch (InterruptedException e) {} textView.setText("end waiting !!!"); }
    – hqt
    Feb 26, 2012 at 3:10
1

@hqt in your sub-question (sorry that I cannot post comment), If you want your program terminate, after you click your button, when your button in 8-seconds-running, you should click again your button, and the "magic" will happened as you wish :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.