Android - Forcing ANR for testing purpose

CelinHC picture CelinHC · Oct 23, 2012 · Viewed 7.8k times · Source

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

Answer

NikkyD picture NikkyD · Oct 23, 2012

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

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