14

Why i can't force Android ANR with this code? No log messages or pop up. The application is just launched lazily.

[UPDATE]

I can't get it even sleeping a View.setOnClickListener or BroadcastReceiver.onReceive!

Is there a trick?

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            Log.e("Test", "", e);
        }
    }
}

I'm using Samsung GT-6200L with stock Android 3.2

CelinHC
  • 1,857
  • 2
  • 27
  • 36
  • Maybe onCreate is not on the UI thread yet, try adding sleep on onStart or onResume – Marcio Covre Oct 23 '12 at 16:21
  • @nininho onCreate cannot be NOT on the UI thread – olshevski Oct 23 '12 at 16:29
  • 1
    There is an option for developers in Android 4.0 - "show all ANRs" or something like this. Maybe you have such an option on Android 3.2? It is turned off by default, so common users don't get disturbed. – olshevski Oct 23 '12 at 16:31
  • Very weird... I can't get it even sleeping a View.setOnClickListener or BroadcastReceiver.onReceive... – CelinHC Oct 23 '12 at 16:40
  • @Kzinch I saw that in ICS/JellyBeans in Setting > Developer Options. Unfortunatelly there isn't this option in 3.2, at least in Samsung GT-6200... – CelinHC Oct 23 '12 at 16:47

6 Answers6

13

Try it in onTouchEvent. In onCreate your activity is not fully running

@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.d(TAG,"onTouchEvent");  
    while(true) {}
}
NikkyD
  • 2,209
  • 1
  • 16
  • 31
  • this has to work. Thread.sleep is always executed and unless interrupted (which you could check via logcat message) it will hold the program and android WILL throw ANR if the program is not responding to any callback within 3 seconds. – NikkyD Oct 24 '12 at 08:37
  • @CelinHC When nothing else happens on the main thread, you will not see an ANR pop up immediately. You have to wait for it or you can trigger it by pressing the back button and wait a few seconds. – Albert-Jan Verhees May 15 '18 at 09:11
  • @Albert-JanVerhees This can be the answer I was expecting for long time. I will test it. – CelinHC May 15 '18 at 16:12
  • because Android gives the app a lot of time to do work if there is nothing it has to respond to (until a threshold is met), but if a response to something is required and the app does not respond an ANR is thrown – NikkyD May 15 '18 at 16:24
5

The ANR-WatchDog project has a test app that produces ANRs in a reliable manner (as reliable as ANRs can be): the app hangs because of a deadlock.

The gist of it:

  1. Prepare a lock object as a private field in your activity: final Object mutex = new Object();

  2. Have a thread that performs some work in a critical section, and an android.os.Handler that posts work depending on the same lock.

    new Thread(new Runnable() {
      @Override
      public void run() {
        synchronized (mutex) {
          while (true) {
            try {
               Thread.sleep(60000);
            }
            catch (InterruptedException e) {
              e.printStackTrace();
            }
          }
        }
      }
    }).start();
    
    new Handler().postDelayed(new Runnable() {
      @Override
      public void run() {
        synchronized (mutex) {
          // Shouldn't happen
          throw new IllegalStateException();
        }
      }
    }, 1000);
    

Putting the above code snippet inside a button click handler, for example, should do the trick.

MrMister
  • 2,456
  • 21
  • 31
3

Try using:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int a=0;
    while(true) {
        a++;
    }       
}

Your code probably didn't work because it got setup too early, and the Activity probably wasn't fully initialized and created yet. With the above code, launch the activity and touch/swipe on the screen and wait for the ANR dialog to popup.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
3

I've been facing the same issue yesterday, and I've found out that using a plain debug build ANR dialogs simply won't show up. (Although the UI thread was completely hanged.)

But after exporting and properly signing the application the dialogs were popped up properly (in every cases mentioned above). However I am still not sure what really prevents to pop up ANR messages, maybe someone else can clarify this later...

peter42
  • 31
  • 1
  • they will show up if you turn this function on in Developers options in the device Settings – alla Jan 27 '20 at 14:36
1

Make a button in your activity.

public void onBtn1(View v) {
   int a = 0;
   while(true) {
      a++;
   }
}

Make the button execute the above code. Spam click the button with your finger =)

hiBrianLee
  • 1,847
  • 16
  • 19
0

I used this code for force ANR

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void force(View view){
        while(true) {}
    }

I just created a simple button in the xml file and set android:onClick=force

nikoo28
  • 2,961
  • 1
  • 29
  • 39