7

I need to reproduce Application Not Responding (ANR) dialogs from Activity and from BroadCastReceiver. I tried to create a simple button click:

 public void makeANRClick(View view){
    while (true);
}

With this code I reproduced ANR on emulator with android 2.3.7. Same code doesn't work on real device with the newest android versions (4+).

Another attempt was as follows:

public void onMakeANRClick(View view){
    try {
        Thread.sleep(15000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

This doesn't help also. Any suggestions?

4 Answers4

1

Keyword multi threading. Please read this topic http://developer.android.com/training/articles/perf-anr.html Also there is no way to manage UI components at Broadcast Reciever, Because it is no UI component. Also there is option in Development Options called "Show All ANR"

Yevgen Kulik
  • 5,713
  • 2
  • 22
  • 44
1

Have a look at StrictMode. And this video, too.

"StrictMode is a developer tool which detects things you might be doing by accident and brings them to your attention so you can fix them. "

galath
  • 5,717
  • 10
  • 29
  • 41
1

Also you can check the dump state to check info about your process https://source.android.com/devices/input/diagnostics.html

I reproduce your code and then pull the 'dumpstate_app_anr.txt.gz' and this was the result

PID TID PR CPU% S VSS RSS PCY UID Thread Proc

15287 15287 0 83% R 227152K 25152K fg u0_a135 a.stackoverflow mx.syca.stackoverflow

07-03 08:46:12.454 1618 1636 I ActivityManager: Killing proc 12946:mx.syca.stackoverflow/u0a135: force stop

It took about 2 minutes to get the ANR dialog

Hope it helps

JARP
  • 1,239
  • 12
  • 12
0

I do believe that the best way to perform ANR in java (dalvik) is to perform absurd ammount of calculations, including function calls.

Perhaps something akin to:

Integer useless = 0;
for (i=2147483648;i<2147483647;i++){
  useless = Math.random() * Math.random() * Math.random() * Math.random();
}

This will at least, trigger some delay, and ANR on weaker systems.

Bonatti
  • 2,778
  • 5
  • 23
  • 42