Accessing UI thread handler from a service

后端 未结 7 1523
被撕碎了的回忆
被撕碎了的回忆 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:44

    This snippet of code constructs a Handler associated with the main (UI) thread:

    Handler handler = new Handler(Looper.getMainLooper());
    

    You can then post stuff for execution in the main (UI) thread like so:

    handler.post(runnable_to_call_from_main_thread);
    

    If the handler itself is created from the main (UI) thread the argument can be omitted for brevity:

    Handler handler = new Handler();
    

    The Android Dev Guide on processes and threads has more information.

提交回复
热议问题