Accessing UI thread handler from a service

后端 未结 7 1551
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 11:57

I am trying some thing new on Android for which I need to access the handler of the UI thread.

I know the following:

  1. The UI thread has its own handler
7条回答
  •  半阙折子戏
    2020-11-27 12:48

    In kotlin thats how you can do it

    Let say if you want to show Toast message from service

    val handler = Handler(Looper.getMainLooper())
    handler.post {
       Toast.makeText(context, "This is my message",Toast.LENGTH_LONG).show()
    }
    

提交回复
热议问题