作为方法参数的处理程序
Handler as a method parameter
为什么有些 api 将处理程序作为方法参数?可能的用例是什么?具体来说,我需要有人来解释为什么我们需要一个处理程序
调度手势方法。我正在尝试使用此 dispatchgesture 方法代表用户执行拖动操作。
boolean dispatchGesture (GestureDescription gesture,
AccessibilityService.GestureResultCallback callback,
Handler handler)
来自 dispatchGesture
doc,
callback AccessibilityService.GestureResultCallback: The object to
call back when the status of the gesture is known. If null, no status
is reported.
handler Handler: The handler on which to call back the
callback object. If null, the object is called back on the service's
main thread.
这基本上意味着,如果您不提供处理程序,那么给定的回调将 运行 在主线程上。
运行 昂贵的 UI 通常不鼓励主线程中的更新程序任务。因此,当您将处理程序作为方法参数传递时,大部分时间都是通过取消一些繁重的任务来减轻 UI 线程,在另一个线程中处理它(处理程序,在这个case) 和 post 完成后的结果。
为什么有些 api 将处理程序作为方法参数?可能的用例是什么?具体来说,我需要有人来解释为什么我们需要一个处理程序 调度手势方法。我正在尝试使用此 dispatchgesture 方法代表用户执行拖动操作。
boolean dispatchGesture (GestureDescription gesture,
AccessibilityService.GestureResultCallback callback,
Handler handler)
来自 dispatchGesture
doc,
callback AccessibilityService.GestureResultCallback: The object to call back when the status of the gesture is known. If null, no status is reported.
handler Handler: The handler on which to call back the callback object. If null, the object is called back on the service's main thread.
这基本上意味着,如果您不提供处理程序,那么给定的回调将 运行 在主线程上。
运行 昂贵的 UI 通常不鼓励主线程中的更新程序任务。因此,当您将处理程序作为方法参数传递时,大部分时间都是通过取消一些繁重的任务来减轻 UI 线程,在另一个线程中处理它(处理程序,在这个case) 和 post 完成后的结果。