Android - Forcing ANR for testing purpose

后端 未结 6 1110
花落未央
花落未央 2020-12-16 03:42

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 V

相关标签:
6条回答
  • 2020-12-16 04:20

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

    0 讨论(0)
  • 2020-12-16 04:28

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

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d(TAG,"onTouchEvent");  
        while(true) {}
    }
    
    0 讨论(0)
  • 2020-12-16 04:31

    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.

    0 讨论(0)
  • 2020-12-16 04:31

    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.

    0 讨论(0)
  • 2020-12-16 04:36

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

    0 讨论(0)
  • 2020-12-16 04:42

    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

    0 讨论(0)
提交回复
热议问题