2017-04-12 74 views
0

我写的一个按钮,单击该按钮时,一个应用程序,下面的方法将被调用:如何处理android输入系统知道输入事件不会导致anr?

public void click(View view) { 
     while (true) { 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

所以,很显然,这会之后的下一个输入事件来引起ANR。我认为这个处理输入事件的click方法不会导致InputEventReceiver类中的finishInputEvent方法被调用,但是我错了,finishInputEvent仍然调用click方法是否返回。在InputEventReceiver类finishInputEvent方法是这样的:

public final void finishInputEvent(InputEvent event, boolean handled) { 
     if (event == null) { 
      throw new IllegalArgumentException("event must not be null"); 
     } 
     if (mReceiverPtr == 0) { 
      Log.w(TAG, "Attempted to finish an input event but the input event " 
        + "receiver has already been disposed."); 
     } else { 
      int index = mSeqMap.indexOfKey(event.getSequenceNumber()); 
      if (index < 0) { 
       Log.w(TAG, "Attempted to finish an input event that is not in progress."); 
      } else { 
       int seq = mSeqMap.valueAt(index); 
       mSeqMap.removeAt(index); 
       nativeFinishInputEvent(mReceiverPtr, seq, handled); 
      } 
     } 
     event.recycleIfNeededAfterDispatch(); 
    } 

我想nativeFinishInputEvent将在输入系统队列中删除一个项目,以便ANR不会被引起的,但现在nativeFinishInputEvent仍然可以调用,为什么会ANR仍然引起?输入系统如何识别输入事件没有处理?

回答

0

所有Android用户界面事件都由给定应用程序的单个线程处理。通过将该线程置于睡眠状态,可以防止线程处理更多事件,并且系统检测到队列中存在未处理事件,即ANR。一般来说,你应该在之间永远不会睡在UI线程中。

+0

我当然知道,我只是想知道输入系统如何知道输入事件没有处理,而finishInputEvent仍然被调用。 – cong

+0

你怎么知道这叫做? – Clyde

+0

我使用演示调试了android源代码。 – cong