Execute code on main thread in Android without access to an Activity?

后端 未结 5 1821
一整个雨季
一整个雨季 2020-12-16 10:32

I have an Android service that starts and maintains a background thread.

From time to time, the background thread needs to do a callback on the main thread. I\'m stu

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-16 11:32

    Your activity has to can bind to the service.

    http://developer.android.com/guide/components/bound-services.html

    Specifically, take a look at creating a Messenger on that page. The client activity can give a messenger object that responds to messages from the service, and once received, run whatever UI code is necessary on the UI thread using a handler.

    DO NOT keep the activity's reference in the service. This can lead to all sorts of memory issues.

提交回复
热议问题