CelinHC
CelinHC

Reputation: 1984

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

Upvotes: 15

Views: 15254

Answers (6)

MrMister
MrMister

Reputation: 2536

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.

Upvotes: 6

nikoo28
nikoo28

Reputation: 2971

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

Upvotes: 0

peter42
peter42

Reputation: 31

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

Upvotes: 3

NikkyD
NikkyD

Reputation: 2284

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

@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.d(TAG,"onTouchEvent");  
    while(true) {}
}

Upvotes: 15

hiBrianLee
hiBrianLee

Reputation: 1857

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 =)

Upvotes: 1

Raghav Sood
Raghav Sood

Reputation: 82583

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.

Upvotes: 4

Related Questions