在Android中需要经常对用户手势进行判断,在判断手势时需要精细的分清楚每个触摸事件以及每个View对事件的接收情况,在View,ViewGroup,Activity中都可以接收事件,在对事件进行处理时onInterceptTouchEvent、dispatchTouchEvent及onTouchEvent这三个函数的调用顺序及关系需要好好理清楚。原理代码有点多,如果不对着具体事例,理解起来很难。下面对着代码进行分析。代码地址为:https://github.com/huangtianyu/DispatchTouchEvent,记得帮忙点Star

MainActivity.java

[java] view plaincopy
  1. package com.zqc.dispatchtouchevent;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.MotionEvent;
  6. import android.view.View;
  7. import static com.zqc.dispatchtouchevent.Constants.TAG;
  8. public class MainActivity extends Activity implements View.OnTouchListener {
  9. private MyView myView;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. Log.e(TAG, "MainActivity onCreate");
  14. setContentView(R.layout.activity_main);
  15. myView = (MyView) findViewById(R.id.myview);
  16. myView.setOnTouchListener(MainActivity.this);
  17. }
  18. @Override
  19. public boolean dispatchTouchEvent(MotionEvent ev) {
  20. Log.e(TAG, "MainActivity dispatchTouchEvent");
  21. return super.dispatchTouchEvent(ev);
  22. }
  23. @Override
  24. public boolean onTouchEvent(MotionEvent event) {
  25. Log.e(TAG, "MainActivity onTouchEvent");
  26. switch (event.getAction()) {
  27. case MotionEvent.ACTION_DOWN:
  28. Log.e(TAG, "MainActivity onTouchEvent ACTION_DOWN");
  29. break;
  30. case MotionEvent.ACTION_MOVE:
  31. Log.e(TAG, "MainActivity onTouchEvent ACTION_MOVE");
  32. break;
  33. case MotionEvent.ACTION_CANCEL:
  34. Log.e(TAG, "MainActivity onTouchEvent ACTION_CANCEL");
  35. break;
  36. case MotionEvent.ACTION_UP:
  37. Log.e(TAG, "MainActivity onTouchEvent ACTION_UP");
  38. break;
  39. default:
  40. Log.e(TAG, "MainActivity onTouchEvent " + event.getAction());
  41. break;
  42. }
  43. return super.onTouchEvent(event);
  44. }
  45. @Override
  46. protected void onResume() {
  47. Log.e(TAG, "MainActivity onResume");
  48. super.onResume();
  49. }
  50. @Override
  51. protected void onPause() {
  52. Log.e(TAG, "MainActivity onPause");
  53. super.onPause();
  54. }
  55. @Override
  56. public boolean onTouch(View v, MotionEvent event) {
  57. Log.e(TAG, "MainActivity onTouch");
  58. switch (event.getAction() & MotionEvent.ACTION_MASK) {
  59. case MotionEvent.ACTION_DOWN:
  60. Log.e(TAG, "MainActivity onTouch ACTION_DOWN");
  61. break;
  62. case MotionEvent.ACTION_MOVE:
  63. Log.e(TAG, "MainActivity onTouch ACTION_MOVE");
  64. break;
  65. case MotionEvent.ACTION_CANCEL:
  66. Log.e(TAG, "MainActivity onTouch ACTION_CANCEL");
  67. break;
  68. case MotionEvent.ACTION_UP:
  69. Log.e(TAG, "MainActivity onTouch ACTION_UP");
  70. break;
  71. default:
  72. Log.e(TAG, "MainActivity onTouchEvent " + event.getAction());
  73. break;
  74. }
  75. return false;
  76. }
  77. }

MyView.java

[java] view plaincopy
  1. package com.zqc.dispatchtouchevent;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.util.Log;
  5. import android.view.GestureDetector;
  6. import android.view.MotionEvent;
  7. import android.widget.TextView;
  8. import static com.zqc.dispatchtouchevent.Constants.MY_GESTURE_TAG;
  9. import static com.zqc.dispatchtouchevent.Constants.TAG;
  10. public class MyView extends TextView {
  11. private Context mContext;
  12. //private GestureDetector mGesture;
  13. public MyView(Context context) {
  14. this(context, null);
  15. }
  16. public MyView(Context context, AttributeSet attrs) {
  17. super(context, attrs);
  18. Log.e(TAG, "MyView");
  19. mContext = context;
  20. //手势初始化
  21. // mGesture = new GestureDetector(mContext, mGestureListener);
  22. }
  23. @Override
  24. public boolean onTouchEvent(MotionEvent event) {
  25. Log.e(TAG, "MyView onTouchEvent");
  26. switch (event.getAction()) {
  27. case MotionEvent.ACTION_DOWN:
  28. Log.e(TAG, "MyView onTouchEvent ACTION_DOWN");
  29. break;
  30. case MotionEvent.ACTION_MOVE:
  31. Log.e(TAG, "MyView onTouchEvent ACTION_MOVE");
  32. break;
  33. case MotionEvent.ACTION_CANCEL:
  34. Log.e(TAG, "MyView onTouchEvent ACTION_CANCEL");
  35. break;
  36. case MotionEvent.ACTION_UP:
  37. Log.e(TAG, "MyView onTouchEvent ACTION_UP");
  38. break;
  39. default:
  40. Log.e(TAG, "MyView onTouchEvent " + event.getAction());
  41. break;
  42. }
  43. //        设置手势监听
  44. // mGesture.onTouchEvent(event);
  45. return super.onTouchEvent(event);
  46. }
  47. @Override
  48. public boolean dispatchTouchEvent(MotionEvent event) {
  49. Log.e(TAG, "MyView dispatchTouchEvent");
  50. return super.dispatchTouchEvent(event);
  51. }
  52. }

MyViewGroup.java

[java] view plaincopy
  1. package com.zqc.dispatchtouchevent;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.util.Log;
  5. import android.view.MotionEvent;
  6. import android.widget.RelativeLayout;
  7. import static com.zqc.dispatchtouchevent.Constants.TAG;
  8. public class MyViewGroup extends RelativeLayout {
  9. public MyViewGroup(Context context) {
  10. this(context, null);
  11. }
  12. public MyViewGroup(Context context, AttributeSet attrs) {
  13. super(context, attrs);
  14. Log.e(TAG, "MyViewGroup");
  15. }
  16. @Override
  17. public boolean onInterceptTouchEvent(MotionEvent ev) {
  18. Log.e(TAG, "MyViewGroup onInterceptTouchEvent");
  19. return super.onInterceptTouchEvent(ev);
  20. }
  21. @Override
  22. public boolean dispatchTouchEvent(MotionEvent ev) {
  23. Log.e(TAG, "MyViewGroup dispatchTouchEvent");
  24. return super.dispatchTouchEvent(ev);
  25. }
  26. @Override
  27. public boolean onTouchEvent(MotionEvent event) {
  28. Log.e(TAG, "MyViewGroup onTouchEvent");
  29. switch (event.getAction()) {
  30. case MotionEvent.ACTION_DOWN:
  31. Log.e(TAG, "MyViewGroup onTouchEvent ACTION_DOWN");
  32. break;
  33. case MotionEvent.ACTION_MOVE:
  34. Log.e(TAG, "MyViewGroup onTouchEvent ACTION_MOVE");
  35. break;
  36. case MotionEvent.ACTION_CANCEL:
  37. Log.e(TAG, "MyViewGroup onTouchEvent ACTION_CANCEL");
  38. break;
  39. case MotionEvent.ACTION_UP:
  40. Log.e(TAG, "MyViewGroup onTouchEvent ACTION_UP");
  41. break;
  42. default:
  43. Log.e(TAG, "MyViewGroup onTouchEvent " + event.getAction());
  44. break;
  45. }
  46. return super.onTouchEvent(event);
  47. }
  48. }

Contants.java

[java] view plaincopy
  1. package com.zqc.dispatchtouchevent;
  2. public class Constants {
  3. public final static String TAG = "MY_LOG";
  4. public final static String MY_GESTURE_TAG = "GESTURE_TAG";
  5. }

在代码中将每个函数分别列出并加上Log输出,这样对着Log日志进行分析,则一目了然。

1.让所有的onInterceptTouchEvent、dispatchTouchEvent及onTouchEvent均返回super.onTouchEvent即均返回false时,轻轻点击MyView然后快速抬起,查看相应的Log:

通过Log能清楚的查看代码执行的流程,具体流程如下:

DOWN事件:MainActivity.dispatchTouchEvent->MyViewGroup.dispatchTouchEvet->MyViewGroup.onInterceptTouchEvent->MyView.dispatchTouchEvent->setOnTouchListener.onTouch->MyView.onTouchEvent->MyViewGroup.onTouchEvent->MainActivity.onTouchEvent

UP事件:MainActivity.dispatchTouchEvent->MainActivity.onTouchEvent

从上面流程可以看出,点击事件最先传给窗口Activity的dispatchTouchEvent函数进行事件分发,然后对于View类,是先传给对应的父View的dispatchTouchEvent进行事件分发,然后在传给里面点击的View。当down事件没有被各个view消费时,最终会调用Acitivity的onTouchEvent,并在在Down后续的UP事件不在传给MyViewGroup和MyView,直接传给MainAcitivity。所以当事件没有被窗口中的View消费时,最终都是给了该窗口Activity类中的onTouchEvent事件处理。从Log中也可以看出setOnTouchListener中的onTouch事件是在对应View的onTouchEvent事件之前被执行。

2.当MainAcivity中dispathTouchEvent返回true时,轻轻点击MyView,查看对应Log:

通过Log可以看到当窗口Activity的dispatchTouchEvent返回true时,DOWN事件没有往View中传,也就没有调用任何的onTouchEvent事件,UP事件也是走到Activity的dispatchTouchEvent时也就结束了。

3.重新置Activity中dispatchTouchEvent返回false,然后置ViewGroup中onInterceptTouchEvent返回true时,轻轻点击MyView查看对应Log:

这时DOWN事件和UP事件的执行流程如下:

DOWN事件:MainActivity.dipatchTouchEvent->MyViewGroup.dispatchTouchEvent->MyViewGroup.onInterceptTouchEvent->MyViewGroup.onTouchEvent->MainActivity.onTouchEvent.

UP事件:MainActiviy.dispatchTouchEvent->MainActivity.onTouchEvent.

从Log中可以看出,当onInterceptTouchEvent返回true时,事件即被MyViewGroup拦截了,这时事件就直接传给MyViewGroup.onTouchEvent,不在往子View传,由于MyViewGroup.onTouchEvent返回的是false,即MyViewGroup并没有消费事件,这时事件会传给窗口Activity,UP事件会传给最后一个接受Down事件的窗口或View。

4.当MyView中onTouchEvent返回true时,即MyView会消费传给他的事件。轻点MyView查看对应的Log:


继续分析DOWN事件的流程:

DOWN事件:MainActivity.dispatchTouchEvent->MyViewGroup.dispatchTouchEvet->MyViewGroup.onInterceptTouchEvent->MyView.dispatchTouchEvent->setOnTouchListener.onTouch->MyView.onTouchEvent

UP事件:MainActivity.dispatchTouchEvent->MyViewGroup.dispatchTouchEvet->MyViewGroup.onInterceptTouchEvent->MyView.dispatchTouchEvent->setOnTouchListener.onTouch->MyView.onTouchEvent

从上面的执行流程可以看出当事件被MyView消费后,事件不会在往上传,后续的UP事件也直接通过dispatchTouchEvent分发给对应的View,这里还是提一下,在MainAcitivy中设置的setOnTouchListener中的onTouch事件是在MyView自身的onTouchEvent事件之前被执行,因而设置的setOnTouchEvent的onTouch函数还是会被执行。

先只分析这几种场景,MOVE事件和UP事件一样只要DOWN事件被某个View消耗了,那么MOVE事件也就直接传到这个View。可以下载代码运行后,在MyView上面滑动下看下Log,具体Log我也贴一份。

情况1:

情况2:

下面对着Android源码来具体分析View的触摸事件到底是怎么执行的。首先根据Log可以最先接收到消息的是Activity的dispatchTouchEvent,在该处设置断点,然后查看对应的调用方法栈(你会发现在调到MainActivity的dispatchTouchEvent时,前面已经调用了很多方法),如下:

由于Android系统启动后会先启动Zygote进程,该进程会在手机开机后一直运行,Android中的几个系统服务都是由Zygote进程fork出来的,一个应用在启动时所分配到的进程也是由Zygote进程fork出来的,通常说一个应用的起点是Application里面的onCreate函数,其实真正的起点是ActivityThread里面的main函数,看到这个main函数是不是有种熟悉的感觉啊。在main函数中初始化了应用程序的主线程,同时初始化了主线程的消息队列,并调用了Looper.loop()函数使主线程不断的对消息队列进行循环检测,有消息则进行处理。点击事件产生一个消息,该消息传到InputEventReceiver后,由InputEventReceiver的继承类WindowInputEventReceiver去处理,WindowInputEventReceiver类是ViewRootImpl类的内部类,查看对应代码如下:

ViewRootImpl.java

[java] view plaincopy
  1. final class WindowInputEventReceiver extends InputEventReceiver {
  2. public WindowInputEventReceiver(InputChannel inputChannel, Looper looper) {
  3. super(inputChannel, looper);
  4. }
  5. @Override
  6. public void onInputEvent(InputEvent event) {
  7. enqueueInputEvent(event, this, 0, true);
  8. }
  9. @Override
  10. public void onBatchedInputEventPending() {
  11. if (mUnbufferedInputDispatch) {
  12. super.onBatchedInputEventPending();
  13. } else {
  14. scheduleConsumeBatchedInput();
  15. }
  16. }
  17. @Override
  18. public void dispose() {
  19. unscheduleConsumeBatchedInput();
  20. super.dispose();
  21. }
  22. }

查看代码可以当点击消息过来时,直接调用ViewRootImpl类中的enqueueInputEvent(event,this,0,true)方法:

ViewRootImpl.java

[java] view plaincopy
  1. void enqueueInputEvent(InputEvent event,
  2. InputEventReceiver receiver, int flags, boolean processImmediately) {
  3. adjustInputEventForCompatibility(event);
  4. QueuedInputEvent q = obtainQueuedInputEvent(event, receiver, flags);
  5. // Always enqueue the input event in order, regardless of its time stamp.
  6. // We do this because the application or the IME may inject key events
  7. // in response to touch events and we want to ensure that the injected keys
  8. // are processed in the order they were received and we cannot trust that
  9. // the time stamp of injected events are monotonic.
  10. QueuedInputEvent last = mPendingInputEventTail;
  11. if (last == null) {
  12. mPendingInputEventHead = q;
  13. mPendingInputEventTail = q;
  14. } else {
  15. last.mNext = q;
  16. mPendingInputEventTail = q;
  17. }
  18. mPendingInputEventCount += 1;
  19. Trace.traceCounter(Trace.TRACE_TAG_INPUT, mPendingInputEventQueueLengthCounterName,
  20. mPendingInputEventCount);
  21. if (processImmediately) {
  22. doProcessInputEvents();
  23. } else {
  24. scheduleProcessInputEvents();
  25. }
  26. }

由于processImmediately为true,因而是立即处理,即直接调用doProcessInputEvents();

ViewRootImpl.java

[java] view plaincopy
  1. void doProcessInputEvents() {
  2. // Deliver all pending input events in the queue.
  3. while (mPendingInputEventHead != null) {
  4. QueuedInputEvent q = mPendingInputEventHead;
  5. mPendingInputEventHead = q.mNext;
  6. if (mPendingInputEventHead == null) {
  7. mPendingInputEventTail = null;
  8. }
  9. q.mNext = null;
  10. mPendingInputEventCount -= 1;
  11. Trace.traceCounter(Trace.TRACE_TAG_INPUT, mPendingInputEventQueueLengthCounterName,
  12. mPendingInputEventCount);
  13. long eventTime = q.mEvent.getEventTimeNano();
  14. long oldestEventTime = eventTime;
  15. if (q.mEvent instanceof MotionEvent) {
  16. MotionEvent me = (MotionEvent)q.mEvent;
  17. if (me.getHistorySize() > 0) {
  18. oldestEventTime = me.getHistoricalEventTimeNano(0);
  19. }
  20. }
  21. mChoreographer.mFrameInfo.updateInputEventTime(eventTime, oldestEventTime);
  22. deliverInputEvent(q);
  23. }
  24. // We are done processing all input events that we can process right now
  25. // so we can clear the pending flag immediately.
  26. if (mProcessInputEventsScheduled) {
  27. mProcessInputEventsScheduled = false;
  28. mHandler.removeMessages(MSG_PROCESS_INPUT_EVENTS);
  29. }
  30. }

z之后调用了deliverInputEvent(q)

ViewRootImpl.java

[java] view plaincopy
  1. private void deliverInputEvent(QueuedInputEvent q) {
  2. Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, "deliverInputEvent",
  3. q.mEvent.getSequenceNumber());
  4. if (mInputEventConsistencyVerifier != null) {
  5. mInputEventConsistencyVerifier.onInputEvent(q.mEvent, 0);
  6. }
  7. InputStage stage;
  8. if (q.shouldSendToSynthesizer()) {
  9. stage = mSyntheticInputStage;
  10. } else {
  11. stage = q.shouldSkipIme() ? mFirstPostImeInputStage : mFirstInputStage;
  12. }
  13. if (stage != null) {
  14. stage.deliver(q);
  15. } else {
  16. finishInputEvent(q);
  17. }
  18. }

在这里初始化了一个InputStage类的实例,然后调用了该类的deliver(q),具体方法如下:

[java] view plaincopy
  1. /**
  2. * Base class for implementing a stage in the chain of responsibility
  3. * for processing input events.
  4. * <p>
  5. * Events are delivered to the stage by the {@link #deliver} method.  The stage
  6. * then has the choice of finishing the event or forwarding it to the next stage.
  7. * </p>
  8. */
  9. abstract class InputStage {
  10. private final InputStage mNext;
  11. protected static final int FORWARD = 0;
  12. protected static final int FINISH_HANDLED = 1;
  13. protected static final int FINISH_NOT_HANDLED = 2;
  14. /**
  15. * Creates an input stage.
  16. * @param next The next stage to which events should be forwarded.
  17. */
  18. public InputStage(InputStage next) {
  19. mNext = next;
  20. }
  21. /**
  22. * Delivers an event to be processed.
  23. */
  24. public final void deliver(QueuedInputEvent q) {
  25. if ((q.mFlags & QueuedInputEvent.FLAG_FINISHED) != 0) {
  26. forward(q);
  27. } else if (shouldDropInputEvent(q)) {
  28. finish(q, false);
  29. } else {
  30. apply(q, onProcess(q));
  31. }
  32. }
  33. /**
  34. * Marks the the input event as finished then forwards it to the next stage.
  35. */
  36. protected void finish(QueuedInputEvent q, boolean handled) {
  37. q.mFlags |= QueuedInputEvent.FLAG_FINISHED;
  38. if (handled) {
  39. q.mFlags |= QueuedInputEvent.FLAG_FINISHED_HANDLED;
  40. }
  41. forward(q);
  42. }
  43. /**
  44. * Forwards the event to the next stage.
  45. */
  46. protected void forward(QueuedInputEvent q) {
  47. onDeliverToNext(q);
  48. }
  49. /**
  50. * Applies a result code from {@link #onProcess} to the specified event.
  51. */
  52. protected void apply(QueuedInputEvent q, int result) {
  53. if (result == FORWARD) {
  54. forward(q);
  55. } else if (result == FINISH_HANDLED) {
  56. finish(q, true);
  57. } else if (result == FINISH_NOT_HANDLED) {
  58. finish(q, false);
  59. } else {
  60. throw new IllegalArgumentException("Invalid result: " + result);
  61. }
  62. }
  63. /**
  64. * Called when an event is ready to be processed.
  65. * @return A result code indicating how the event was handled.
  66. */
  67. protected int onProcess(QueuedInputEvent q) {
  68. return FORWARD;
  69. }
  70. /**
  71. * Called when an event is being delivered to the next stage.
  72. */
  73. protected void onDeliverToNext(QueuedInputEvent q) {
  74. if (DEBUG_INPUT_STAGES) {
  75. Log.v(TAG, "Done with " + getClass().getSimpleName() + ". " + q);
  76. }
  77. if (mNext != null) {
  78. mNext.deliver(q);
  79. } else {
  80. finishInputEvent(q);
  81. }
  82. }
  83. protected boolean shouldDropInputEvent(QueuedInputEvent q) {
  84. if (mView == null || !mAdded) {
  85. Slog.w(TAG, "Dropping event due to root view being removed: " + q.mEvent);
  86. return true;
  87. } else if ((!mAttachInfo.mHasWindowFocus
  88. && !q.mEvent.isFromSource(InputDevice.SOURCE_CLASS_POINTER)) || mStopped
  89. || (mPausedForTransition && !isBack(q.mEvent))) {
  90. // This is a focus event and the window doesn't currently have input focus or
  91. // has stopped. This could be an event that came back from the previous stage
  92. // but the window has lost focus or stopped in the meantime.
  93. if (isTerminalInputEvent(q.mEvent)) {
  94. // Don't drop terminal input events, however mark them as canceled.
  95. q.mEvent.cancel();
  96. Slog.w(TAG, "Cancelling event due to no window focus: " + q.mEvent);
  97. return false;
  98. }
  99. // Drop non-terminal input events.
  100. Slog.w(TAG, "Dropping event due to no window focus: " + q.mEvent);
  101. return true;
  102. }
  103. return false;
  104. }
  105. void dump(String prefix, PrintWriter writer) {
  106. if (mNext != null) {
  107. mNext.dump(prefix, writer);
  108. }
  109. }
  110. private boolean isBack(InputEvent event) {
  111. if (event instanceof KeyEvent) {
  112. return ((KeyEvent) event).getKeyCode() == KeyEvent.KEYCODE_BACK;
  113. } else {
  114. return false;
  115. }
  116. }
  117. }

对应方法栈可以看出,进过一些列调用最终会调用到ViewPostImeInputStage类的processPointerEvent方法.

ViewRootImpl.java

[java] view plaincopy
  1. private int processPointerEvent(QueuedInputEvent q) {
  2. final MotionEvent event = (MotionEvent)q.mEvent;
  3. mAttachInfo.mUnbufferedDispatchRequested = false;
  4. boolean handled = mView.dispatchPointerEvent(event);
  5. if (mAttachInfo.mUnbufferedDispatchRequested && !mUnbufferedInputDispatch) {
  6. mUnbufferedInputDispatch = true;
  7. if (mConsumeBatchedInputScheduled) {
  8. scheduleConsumeBatchedInputImmediately();
  9. }
  10. }
  11. return handled ? FINISH_HANDLED : FORWARD;
  12. }

在该方法中调用了mView的dispatchPointerEvent,这个mView的初始化可以查看Activity的创建代码,在Activity创建的时候会给Activity设置一个根布局也就是DecorView,这里的mView就是DecorView,这个DecorView是PhoneWindow的私有内部类,它继承于FrameLayout并实现了RootViewSurfaceTaker接口,但是该方法是View类的一个final方法,子类无法覆写,直接查看View中的相应代码即可。代码如下:

View.java

[java] view plaincopy
  1. /**
  2. * Dispatch a pointer event.
  3. * <p>
  4. * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
  5. * other events to {@link #onGenericMotionEvent(MotionEvent)}.  This separation of concerns
  6. * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
  7. * and should not be expected to handle other pointing device features.
  8. * </p>
  9. *
  10. * @param event The motion event to be dispatched.
  11. * @return True if the event was handled by the view, false otherwise.
  12. * @hide
  13. */
  14. public final boolean dispatchPointerEvent(MotionEvent event) {
  15. if (event.isTouchEvent()) {
  16. return dispatchTouchEvent(event);
  17. } else {
  18. return dispatchGenericMotionEvent(event);
  19. }
  20. }

继续查看DecorView类中的dispatchTouchEvent方法,代码如下:

PhoneWindow.java

[java] view plaincopy
  1. @Override
  2. public boolean dispatchTouchEvent(MotionEvent ev) {
  3. final Callback cb = getCallback();
  4. return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchTouchEvent(ev)
  5. : super.dispatchTouchEvent(ev);
  6. }

这个getCallback也就是当前的Activity,当当前Activity没有destroy的时候即调用该Activity的dispatchTouchEvent,这里代码就回到了应用层了,框架层完成了很多操作,这些操作只有查看源码才知道,这里终于回到了我们编写代码的地方了。当然这之后还是会通过框架层将对应的Touch事件传给对应的ViewGroup和View。下面先看下Activity中dispatchTouchEvent的代码:

Activity.java

[java] view plaincopy
  1. /**
  2. * Called to process touch screen events.  You can override this to
  3. * intercept all touch screen events before they are dispatched to the
  4. * window.  Be sure to call this implementation for touch screen events
  5. * that should be handled normally.
  6. *
  7. * @param ev The touch screen event.
  8. *
  9. * @return boolean Return true if this event was consumed.
  10. */
  11. public boolean dispatchTouchEvent(MotionEvent ev) {
  12. if (ev.getAction() == MotionEvent.ACTION_DOWN) {
  13. onUserInteraction();
  14. }
  15. if (getWindow().superDispatchTouchEvent(ev)) {//这个getWindow就是PhoneWindow,也就是通过PhoneWindow继续对touch事件进行分发。
  16. return true;
  17. }//当上面返回true,也就是View把事件消费了,那么就不再调用Activity的onTouchEvent函数了。
  18. return onTouchEvent(ev);
  19. }

果然这里又回到了框架层,这里getWindow就是PhoneWindow,继续查看PhoneWindow的代码:

PhoneWindow.java

[java] view plaincopy
  1. @Override
  2. public boolean superDispatchTouchEvent(MotionEvent event) {
  3. return mDecor.superDispatchTouchEvent(event);
  4. }

这里把事件就传给了DecorView进行分发。

PhoneWindow.java->DecorView

[java] view plaincopy
  1. public boolean superDispatchTouchEvent(MotionEvent event) {
  2. return super.dispatchTouchEvent(event);
  3. }

前面说过DecorView继承于FrameLayout,这里super.dispatchTouchEvent就是调用了FrameLayout里面的dispatchTouchEvent,而FrameLayout类中并未重写dispatchTouchEvent,因而直接调用的是ViewGroup中的dispatchTouchEvent。继续查看代码:

ViewGroup.java

[java] view plaincopy
  1. /**
  2. * {@inheritDoc}
  3. */
  4. @Override
  5. public boolean dispatchTouchEvent(MotionEvent ev) {
  6. if (mInputEventConsistencyVerifier != null) {
  7. mInputEventConsistencyVerifier.onTouchEvent(ev, 1);
  8. }
  9. // If the event targets the accessibility focused view and this is it, start
  10. // normal event dispatch. Maybe a descendant is what will handle the click.
  11. if (ev.isTargetAccessibilityFocus() && isAccessibilityFocusedViewOrHost()) {
  12. ev.setTargetAccessibilityFocus(false);
  13. }
  14. boolean handled = false;
  15. if (onFilterTouchEventForSecurity(ev)) {
  16. final int action = ev.getAction();
  17. final int actionMasked = action & MotionEvent.ACTION_MASK;
  18. // Handle an initial down.
  19. if (actionMasked == MotionEvent.ACTION_DOWN) {
  20. // Throw away all previous state when starting a new touch gesture.
  21. // The framework may have dropped the up or cancel event for the previous gesture
  22. // due to an app switch, ANR, or some other state change.
  23. cancelAndClearTouchTargets(ev);
  24. resetTouchState();
  25. }
  26. // Check for interception.
  27. final boolean intercepted;
  28. if (actionMasked == MotionEvent.ACTION_DOWN
  29. || mFirstTouchTarget != null) {
  30. final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
  31. if (!disallowIntercept) {
  32. intercepted = onInterceptTouchEvent(ev);
  33. ev.setAction(action); // restore action in case it was changed
  34. } else {
  35. intercepted = false;
  36. }
  37. } else {
  38. // There are no touch targets and this action is not an initial down
  39. // so this view group continues to intercept touches.
  40. intercepted = true;
  41. }
  42. // If intercepted, start normal event dispatch. Also if there is already
  43. // a view that is handling the gesture, do normal event dispatch.
  44. if (intercepted || mFirstTouchTarget != null) {
  45. ev.setTargetAccessibilityFocus(false);
  46. }
  47. // Check for cancelation.
  48. final boolean canceled = resetCancelNextUpFlag(this)
  49. || actionMasked == MotionEvent.ACTION_CANCEL;
  50. // Update list of touch targets for pointer down, if needed.
  51. final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;
  52. TouchTarget newTouchTarget = null;
  53. boolean alreadyDispatchedToNewTouchTarget = false;
  54. if (!canceled && !intercepted) {
  55. // If the event is targeting accessiiblity focus we give it to the
  56. // view that has accessibility focus and if it does not handle it
  57. // we clear the flag and dispatch the event to all children as usual.
  58. // We are looking up the accessibility focused host to avoid keeping
  59. // state since these events are very rare.
  60. View childWithAccessibilityFocus = ev.isTargetAccessibilityFocus()
  61. ? findChildWithAccessibilityFocus() : null;
  62. if (actionMasked == MotionEvent.ACTION_DOWN
  63. || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
  64. || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
  65. final int actionIndex = ev.getActionIndex(); // always 0 for down
  66. final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex)
  67. : TouchTarget.ALL_POINTER_IDS;
  68. // Clean up earlier touch targets for this pointer id in case they
  69. // have become out of sync.
  70. removePointersFromTouchTargets(idBitsToAssign);
  71. final int childrenCount = mChildrenCount;
  72. if (newTouchTarget == null && childrenCount != 0) {
  73. final float x = ev.getX(actionIndex);
  74. final float y = ev.getY(actionIndex);
  75. // Find a child that can receive the event.
  76. // Scan children from front to back.
  77. final ArrayList<View> preorderedList = buildOrderedChildList();
  78. final boolean customOrder = preorderedList == null
  79. && isChildrenDrawingOrderEnabled();
  80. final View[] children = mChildren;
  81. for (int i = childrenCount - 1; i >= 0; i--) {
  82. final int childIndex = customOrder
  83. ? getChildDrawingOrder(childrenCount, i) : i;
  84. final View child = (preorderedList == null)
  85. ? children[childIndex] : preorderedList.get(childIndex);
  86. // If there is a view that has accessibility focus we want it
  87. // to get the event first and if not handled we will perform a
  88. // normal dispatch. We may do a double iteration but this is
  89. // safer given the timeframe.
  90. if (childWithAccessibilityFocus != null) {
  91. if (childWithAccessibilityFocus != child) {
  92. continue;
  93. }
  94. childWithAccessibilityFocus = null;
  95. i = childrenCount - 1;
  96. }
  97. if (!canViewReceivePointerEvents(child)
  98. || !isTransformedTouchPointInView(x, y, child, null)) {
  99. ev.setTargetAccessibilityFocus(false);
  100. continue;
  101. }
  102. newTouchTarget = getTouchTarget(child);
  103. if (newTouchTarget != null) {
  104. // Child is already receiving touch within its bounds.
  105. // Give it the new pointer in addition to the ones it is handling.
  106. newTouchTarget.pointerIdBits |= idBitsToAssign;
  107. break;
  108. }
  109. resetCancelNextUpFlag(child);
  110. if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
  111. // Child wants to receive touch within its bounds.
  112. mLastTouchDownTime = ev.getDownTime();
  113. if (preorderedList != null) {
  114. // childIndex points into presorted list, find original index
  115. for (int j = 0; j < childrenCount; j++) {
  116. if (children[childIndex] == mChildren[j]) {
  117. mLastTouchDownIndex = j;
  118. break;
  119. }
  120. }
  121. } else {
  122. mLastTouchDownIndex = childIndex;
  123. }
  124. mLastTouchDownX = ev.getX();
  125. mLastTouchDownY = ev.getY();
  126. newTouchTarget = addTouchTarget(child, idBitsToAssign);
  127. alreadyDispatchedToNewTouchTarget = true;
  128. break;
  129. }
  130. // The accessibility focus didn't handle the event, so clear
  131. // the flag and do a normal dispatch to all children.
  132. ev.setTargetAccessibilityFocus(false);
  133. }
  134. if (preorderedList != null) preorderedList.clear();
  135. }
  136. if (newTouchTarget == null && mFirstTouchTarget != null) {
  137. // Did not find a child to receive the event.
  138. // Assign the pointer to the least recently added target.
  139. newTouchTarget = mFirstTouchTarget;
  140. while (newTouchTarget.next != null) {
  141. newTouchTarget = newTouchTarget.next;
  142. }
  143. newTouchTarget.pointerIdBits |= idBitsToAssign;
  144. }
  145. }
  146. }
  147. // Dispatch to touch targets.
  148. if (mFirstTouchTarget == null) {
  149. // No touch targets so treat this as an ordinary view.
  150. handled = dispatchTransformedTouchEvent(ev, canceled, null,
  151. TouchTarget.ALL_POINTER_IDS);
  152. } else {
  153. // Dispatch to touch targets, excluding the new touch target if we already
  154. // dispatched to it.  Cancel touch targets if necessary.
  155. TouchTarget predecessor = null;
  156. TouchTarget target = mFirstTouchTarget;
  157. while (target != null) {
  158. final TouchTarget next = target.next;
  159. if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
  160. handled = true;
  161. } else {
  162. final boolean cancelChild = resetCancelNextUpFlag(target.child)
  163. || intercepted;
  164. if (dispatchTransformedTouchEvent(ev, cancelChild,
  165. target.child, target.pointerIdBits)) {
  166. handled = true;
  167. }
  168. if (cancelChild) {
  169. if (predecessor == null) {
  170. mFirstTouchTarget = next;
  171. } else {
  172. predecessor.next = next;
  173. }
  174. target.recycle();
  175. target = next;
  176. continue;
  177. }
  178. }
  179. predecessor = target;
  180. target = next;
  181. }
  182. }
  183. // Update list of touch targets for pointer up or cancel, if needed.
  184. if (canceled
  185. || actionMasked == MotionEvent.ACTION_UP
  186. || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
  187. resetTouchState();
  188. } else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) {
  189. final int actionIndex = ev.getActionIndex();
  190. final int idBitsToRemove = 1 << ev.getPointerId(actionIndex);
  191. removePointersFromTouchTargets(idBitsToRemove);
  192. }
  193. }
  194. if (!handled && mInputEventConsistencyVerifier != null) {
  195. mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1);
  196. }
  197. return handled;
  198. }

代码有点多,通过调试可知将会调用dispatchTransformedTouchEvent,查看代码如下:

ViewGroup.java

[java] view plaincopy
  1. /**
  2. * Transforms a motion event into the coordinate space of a particular child view,
  3. * filters out irrelevant pointer ids, and overrides its action if necessary.
  4. * If child is null, assumes the MotionEvent will be sent to this ViewGroup instead.
  5. */
  6. private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
  7. View child, int desiredPointerIdBits) {
  8. final boolean handled;
  9. // Canceling motions is a special case.  We don't need to perform any transformations
  10. // or filtering.  The important part is the action, not the contents.
  11. final int oldAction = event.getAction();
  12. if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {
  13. event.setAction(MotionEvent.ACTION_CANCEL);
  14. if (child == null) {
  15. handled = super.dispatchTouchEvent(event);
  16. } else {
  17. handled = child.dispatchTouchEvent(event);
  18. }
  19. event.setAction(oldAction);
  20. return handled;
  21. }
  22. // Calculate the number of pointers to deliver.
  23. final int oldPointerIdBits = event.getPointerIdBits();
  24. final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits;
  25. // If for some reason we ended up in an inconsistent state where it looks like we
  26. // might produce a motion event with no pointers in it, then drop the event.
  27. if (newPointerIdBits == 0) {
  28. return false;
  29. }
  30. // If the number of pointers is the same and we don't need to perform any fancy
  31. // irreversible transformations, then we can reuse the motion event for this
  32. // dispatch as long as we are careful to revert any changes we make.
  33. // Otherwise we need to make a copy.
  34. final MotionEvent transformedEvent;
  35. if (newPointerIdBits == oldPointerIdBits) {
  36. if (child == null || child.hasIdentityMatrix()) {
  37. if (child == null) {
  38. handled = super.dispatchTouchEvent(event);
  39. } else {
  40. final float offsetX = mScrollX - child.mLeft;
  41. final float offsetY = mScrollY - child.mTop;
  42. event.offsetLocation(offsetX, offsetY);
  43. handled = child.dispatchTouchEvent(event);
  44. event.offsetLocation(-offsetX, -offsetY);
  45. }
  46. return handled;
  47. }
  48. transformedEvent = MotionEvent.obtain(event);
  49. } else {
  50. transformedEvent = event.split(newPointerIdBits);
  51. }
  52. // Perform any necessary transformations and dispatch.
  53. if (child == null) {
  54. handled = super.dispatchTouchEvent(transformedEvent);
  55. } else {
  56. final float offsetX = mScrollX - child.mLeft;
  57. final float offsetY = mScrollY - child.mTop;
  58. transformedEvent.offsetLocation(offsetX, offsetY);
  59. if (! child.hasIdentityMatrix()) {
  60. transformedEvent.transform(child.getInverseMatrix());
  61. }
  62. handled = child.dispatchTouchEvent(transformedEvent);
  63. }
  64. // Done.
  65. transformedEvent.recycle();
  66. return handled;
  67. }

在该函数中调用了child.dispatchTouchEvent(),这里便走到了子View的dispatchTouchEvent中。子View也就是MyView,也就走到了TextView的dispathTouchEvent中,由于TextView并未重写dispathTouchEvent,因而直接进入View的dispatchTouchEvent中,代码如下:

View.java

[java] view plaincopy
  1. /**
  2. * Pass the touch screen motion event down to the target view, or this
  3. * view if it is the target.
  4. *
  5. * @param event The motion event to be dispatched.
  6. * @return True if the event was handled by the view, false otherwise.
  7. */
  8. public boolean dispatchTouchEvent(MotionEvent event) {
  9. // If the event should be handled by accessibility focus first.
  10. if (event.isTargetAccessibilityFocus()) {
  11. // We don't have focus or no virtual descendant has it, do not handle the event.
  12. if (!isAccessibilityFocusedViewOrHost()) {
  13. return false;
  14. }
  15. // We have focus and got the event, then use normal event dispatch.
  16. event.setTargetAccessibilityFocus(false);
  17. }
  18. boolean result = false;
  19. if (mInputEventConsistencyVerifier != null) {
  20. mInputEventConsistencyVerifier.onTouchEvent(event, 0);
  21. }
  22. final int actionMasked = event.getActionMasked();
  23. if (actionMasked == MotionEvent.ACTION_DOWN) {
  24. // Defensive cleanup for new gesture
  25. stopNestedScroll();
  26. }
  27. if (onFilterTouchEventForSecurity(event)) {
  28. //noinspection SimplifiableIfStatement
  29. ListenerInfo li = mListenerInfo;
  30. if (li != null && li.mOnTouchListener != null
  31. && (mViewFlags & ENABLED_MASK) == ENABLED
  32. && li.mOnTouchListener.onTouch(this, event)) {//在这里就调用了setOnTouchListener中的onTouch函数,如果有一个消费了,那么result=true
  33. result = true;
  34. }
  35. if (!result && onTouchEvent(event)) {//当上面的result为true时,子View的onTouchEvent便不会执行了。
  36. result = true;
  37. }
  38. }
  39. if (!result && mInputEventConsistencyVerifier != null) {
  40. mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
  41. }
  42. // Clean up after nested scrolls if this is the end of a gesture;
  43. // also cancel it if we tried an ACTION_DOWN but we didn't want the rest
  44. // of the gesture.
  45. if (actionMasked == MotionEvent.ACTION_UP ||
  46. actionMasked == MotionEvent.ACTION_CANCEL ||
  47. (actionMasked == MotionEvent.ACTION_DOWN && !result)) {
  48. stopNestedScroll();
  49. }
  50. return result;
  51. }

在该函数中看到了在MainActivity中设置的setOnTouchListener对应的Listener接口,当setListener中的onTouch返回true时,MyView本身的onTouchEvent便不被调用。接下来看下View的onTouchEvent代码:

View.java

[java] view plaincopy
  1. public boolean onTouchEvent(MotionEvent event) {
  2. final float x = event.getX();
  3. final float y = event.getY();
  4. final int viewFlags = mViewFlags;
  5. final int action = event.getAction();
  6. if ((viewFlags & ENABLED_MASK) == DISABLED) {
  7. if (action == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
  8. setPressed(false);
  9. }
  10. // A disabled view that is clickable still consumes the touch
  11. // events, it just doesn't respond to them.
  12. return (((viewFlags & CLICKABLE) == CLICKABLE
  13. || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
  14. || (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE);
  15. }
  16. if (mTouchDelegate != null) {//一个View还可以设置TouchDelegate,也可以在TouchDelegate的onTouchEvent里面处理点击事件
  17. if (mTouchDelegate.onTouchEvent(event)) {
  18. return true;
  19. }
  20. }
  21. if (((viewFlags & CLICKABLE) == CLICKABLE ||
  22. (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) ||
  23. (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE) {
  24. switch (action) {
  25. case MotionEvent.ACTION_UP:
  26. boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
  27. if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
  28. // take focus if we don't have it already and we should in
  29. // touch mode.
  30. boolean focusTaken = false;
  31. if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
  32. focusTaken = requestFocus();
  33. }
  34. if (prepressed) {
  35. // The button is being released before we actually
  36. // showed it as pressed.  Make it show the pressed
  37. // state now (before scheduling the click) to ensure
  38. // the user sees it.
  39. setPressed(true, x, y);
  40. }
  41. if (!mHasPerformedLongPress && !mIgnoreNextUpEvent) {
  42. // This is a tap, so remove the longpress check
  43. removeLongPressCallback();
  44. // Only perform take click actions if we were in the pressed state
  45. if (!focusTaken) {
  46. // Use a Runnable and post this rather than calling
  47. // performClick directly. This lets other visual state
  48. // of the view update before click actions start.
  49. if (mPerformClick == null) {
  50. mPerformClick = new PerformClick();
  51. }
  52. if (!post(mPerformClick)) {
  53. performClick();
  54. }
  55. }
  56. }
  57. if (mUnsetPressedState == null) {
  58. mUnsetPressedState = new UnsetPressedState();
  59. }
  60. if (prepressed) {
  61. postDelayed(mUnsetPressedState,
  62. ViewConfiguration.getPressedStateDuration());
  63. } else if (!post(mUnsetPressedState)) {
  64. // If the post failed, unpress right now
  65. mUnsetPressedState.run();
  66. }
  67. removeTapCallback();
  68. }
  69. mIgnoreNextUpEvent = false;
  70. break;
  71. case MotionEvent.ACTION_DOWN:
  72. mHasPerformedLongPress = false;
  73. if (performButtonActionOnTouchDown(event)) {
  74. break;
  75. }
  76. // Walk up the hierarchy to determine if we're inside a scrolling container.
  77. boolean isInScrollingContainer = isInScrollingContainer();
  78. // For views inside a scrolling container, delay the pressed feedback for
  79. // a short period in case this is a scroll.
  80. if (isInScrollingContainer) {
  81. mPrivateFlags |= PFLAG_PREPRESSED;
  82. if (mPendingCheckForTap == null) {
  83. mPendingCheckForTap = new CheckForTap();
  84. }
  85. mPendingCheckForTap.x = event.getX();
  86. mPendingCheckForTap.y = event.getY();
[java] view plaincopy
  1. //这个注意下,这里会调用ViewRootImpl内部函数也就是后面的MOVE为啥知道前面DOWN了

postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout()); } else { // Not inside a scrolling container, so show the feedback right away setPressed(true, x, y);

[java] view plaincopy
  1. //这个去检查是否有长按事件

checkForLongClick(0); } break; case MotionEvent.ACTION_CANCEL: setPressed(false); removeTapCallback(); removeLongPressCallback(); mInContextButtonPress = false; mHasPerformedLongPress = false; mIgnoreNextUpEvent = false; break; case MotionEvent.ACTION_MOVE: drawableHotspotChanged(x, y); // Be lenient about moving outside of buttons if (!pointInView(x, y, mTouchSlop)) { // Outside button removeTapCallback(); if ((mPrivateFlags & PFLAG_PRESSED) != 0) { // Remove any future long press/tap checks removeLongPressCallback(); setPressed(false); } } break; } return true; } return false; }这里仅分析下DOWN事件的处理,这里会先处理按钮自身的一些事件,具体事件见如下代码:

[java] view plaincopy
  1. /**
  2. * Performs button-related actions during a touch down event.
  3. *
  4. * @param event The event.
  5. * @return True if the down was consumed.
  6. *
  7. * @hide
  8. */
  9. protected boolean performButtonActionOnTouchDown(MotionEvent event) {
  10. if (event.getToolType(0) == MotionEvent.TOOL_TYPE_MOUSE &&
  11. (event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
  12. showContextMenu(event.getX(), event.getY(), event.getMetaState());
  13. mPrivateFlags |= PFLAG_CANCEL_NEXT_UP_EVENT;
  14. return true;
  15. }
  16. return false;
  17. }

然后判断当前View的父View是否在滚动,如果不在滚动就调用postDelayed:

View.java

[java] view plaincopy
  1. public boolean postDelayed(Runnable action, long delayMillis) {
  2. final AttachInfo attachInfo = mAttachInfo;
  3. if (attachInfo != null) {
  4. return attachInfo.mHandler.postDelayed(action, delayMillis);
  5. }
  6. // Assume that post will succeed later
  7. ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
  8. return true;
  9. }

将action延迟一段时间,用于后续判断(是否长按事件,后续MOVE事件,UP事件)。

转载于:https://www.cnblogs.com/Free-Thinker/p/8915919.html

Android中onInterceptTouchEvent、dispatchTouchEvent及onTouchEvent的调用顺序及内部原理相关推荐

  1. 多继承中构造器和析构器的调用顺序

    多继承中构造器和析构器的调用顺序: 构造器的调用顺序就像盖房子,从最基层开始: 析构器的调用顺序就像拆房子,从最顶层开始:

  2. Android中的dispatchTouchEvent()、onInterceptTouchEvent()和onTouchEvent()

    http://blog.csdn.net/xyz_lmn/article/details/12517911 Android中触摸事件传递过程中最重要的是dispatchTouchEvent().onI ...

  3. Unity3D中关于场景销毁时事件调用顺序的一点记录

    先说一下我遇到的问题,我弄了一个对象池管理多个对象,对象池绑定在一个GameObject上,每个对象在OnBecameInvisible时会进行回收(即移出屏幕就回收),但是当场景切换或停止运行程序时 ...

  4. java中构造代码块、方法调用顺序问题

    1. 继承的概念 继承在本职上是特殊--一般的关系,即常说的is-a关系.子类继承父类,表明子类是一种特殊的父类,并且具有父类所不具有的一些属性或方法. 2. 继承中的初始化顺序 从类的结构上而言,其 ...

  5. Android中实现「类方法指令抽取方式」加固方案原理解析

    一.前言 Android中加固方案一直在进步,因为新的加固方案出来就会被人无情的破解脱壳了,从第一代加固方案落地加密dex文件,第二代加固方案不落地加密dex文件,在到第三代加固方案类方法抽取,以后后 ...

  6. 关于Android中的onCreate()多次被调用导致bindService被多次调用的问题...

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010046908/article/details/50511776                 ...

  7. Android中so文件的生成和调用

    1.so文件介绍    "so"文件是使用C/C++编写生成的,在Android 平台上快速编译.打包该文件,它是一个动态链接库,而生成"so"文件其实就是JN ...

  8. android方法是对象吗,为什么android中对象不初始化也能调用方法?

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 public class MainActivity extends ActionBarActivity { private Button bt; priv ...

  9. android 调用短信,android中可以通过两种方式调用接口发送短信

    第一:调用系统短信接口直接发送短信:主要代码如下: //直接调用短信接口发短信 SmsManager smsManager = SmsManager.getDefault(); List divide ...

最新文章

  1. Oracle数据库的显示提交与隐式提交,针对oracle工具的自动提交机制
  2. numpy.mod详解
  3. JavaScript高级day01-AM【WebStrom安装、数据类型分类及判断、数据-内存-变量、引用变量赋值、对象的组成】
  4. Vue 快速集成ElementUI
  5. JSP---网页日历
  6. android之socket编程实例
  7. Unity 中从3D到Universal RP配置方法
  8. Codeforces 592 A. PawnChess 【Codeforces Round #328 (Div. 2)】
  9. AsyncTask我来给你扯会蛋
  10. Gym 101246(ACM ICPC 2010-2011, NEERC, Southern Subregional Contest Russia, Saratov)
  11. 顶级赛事!2021 CCF大数据与计算智能大赛重磅开赛!
  12. iphone怎样关闭副屏_机情烩:联通eSIM主副卡业务上线 副卡套餐最低仅10元
  13. mysql出现2058,连接MySQL报“Error No.2058 Plugin caching_sha2_password could not be loaded”
  14. java通过aspose实现文档间格式转换
  15. powershell 结束进程的四种写法
  16. 郭德纲被新浪删掉的博文《人在江湖》,骂功了得
  17. 使用2节点梁或梁/杆单元分析弹塑性梁或框架(python,有限元)
  18. P44-前端基础CSS-Position相对定位介绍
  19. 数字模拟电路课程设计multisim仿真源文件和设计原理
  20. python输出假分数_解析ArcGis的标注(一)——先看看分数式、假分数式标注是怎样实现的...

热门文章

  1. 每天一道LeetCode-----将数字用字母表示(本质是26进制转换)
  2. keepalived(5)——lvs和arrp集成操作(1)
  3. html(5)标签form表单——进阶
  4. 内存对齐指令详解(posix_memalign)
  5. 解决 iOS 12.4 Killed: 9 的问题
  6. Redis 总结精讲 看一篇成高手系统 四
  7. HDU 1568 Fibonacci
  8. 摸透 Redis 主从复制、哨兵、Cluster 三种模式
  9. shared_ptr和new结合使用的几个简单例子
  10. linux 中 alien命令的使用