Android - Forcing ANR for testing purpose

冷暖自知 提交于 2019-12-09 06:19:13

问题


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


回答1:


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

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



回答2:


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




回答3:


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.




回答4:


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




回答5:


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




回答6:


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.



来源:https://stackoverflow.com/questions/13034837/android-forcing-anr-for-testing-purpose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!