Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Forcing ANR for testing purpose

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

like image 256
CelinHC Avatar asked Oct 23 '12 16:10

CelinHC


4 Answers

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.

like image 52
MrMister Avatar answered Sep 18 '22 08:09

MrMister


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

@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.d(TAG,"onTouchEvent");  
    while(true) {}
}
like image 31
NikkyD Avatar answered Sep 17 '22 08:09

NikkyD


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

like image 21
peter42 Avatar answered Sep 20 '22 08:09

peter42


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.

like image 29
Raghav Sood Avatar answered Sep 17 '22 08:09

Raghav Sood