Why some api have handler as a method parameter? What are the possible use cases? Specifically I need someone to explain why we need a handler in the dispatchgesture method. I am trying to perform drag operation on behalf of user using this dispatchgesture method.
boolean dispatchGesture (GestureDescription gesture,
AccessibilityService.GestureResultCallback callback,
Handler handler)
From 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.
That basically means, if you don't provide a Handler then the given callback will run on main thread.
Running expensive UI updater task in main thread in general is always discouraged. So, when you're passing a handler as method param, most of the time it is there to lighten the UI thread up by taking off some of the heavy duty tasks, process it in another thread(handler, in this case) and post the result when its done.