【问题标题】:how to get the Handler of the main ui thread?如何获取主ui线程的Handler?
【发布时间】:2018-01-25 10:57:56
【问题描述】:

使用SurfaceTexture我需要调用这个函数:

setOnFrameAvailableListener (SurfaceTexture.OnFrameAvailableListener listener, 
                Handler handler)

想给主UI线程的Handler,但是不知道怎么弄……

注意:我知道我可以做到Handler handler = new Handler(Looper.getMainLooper());,但它们不是已经创建的 Handler 对象,可在某处访问?

【问题讨论】:

  • Handler handler = new Handler(Looper.getMainLooper());
  • 感谢@BapusahebShinde - 但它们不是已经创建的 Handler 对象,可在某处访问?
  • 不,没有默认用户界面Handler,你是从某个背景Thread 调用setOnFrameAvailableListener 吗?
  • 不,实际上我是从主 ui 线程调用它,但文档说我们必须提供一个处理程序,否则 OnFrameAvailableListener 将在任意线程上调用:(
  • 那么如果你传递了null,它将在同一个ui线程上被调用——见here

标签: android android-handler


【解决方案1】:
HandlerThread handlerThread = new HandlerThread("BGHandler") // string here can be any name
handlerThread.start()
surfaceTexture.setOnFrameAvailableListener(this, Handler(handlerThread.getLooper())) //this means that the class calling this function implements the interface, I am sure u understand that since you have not asked for it, but if necessary I can elaborate more, just ask in the comments

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 2018-03-04
    相关资源
    最近更新 更多