Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reproduce Application Not Responding (ANR) from Activity and from BroadcastReceiver

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?

like image 937
Alexander Myznikov Avatar asked Jul 03 '15 13:07

Alexander Myznikov


2 Answers

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. "

like image 61
galath Avatar answered Oct 08 '22 00:10

galath


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

like image 39
JARP Avatar answered Oct 07 '22 23:10

JARP