How to call methods of a Service from activity?

前端 未结 6 1332
不知归路
不知归路 2020-11-29 07:22

I simply want to call methods of a local service from my activity. How can i do that ?

6条回答
  •  没有蜡笔的小新
    2020-11-29 07:51

    One way to do this is by defining an interface with Android's AIDL and making use of the Binder subsystem to perform IPC. There is a great set of instructions at the link I posted. I'd start there and then post here if you have questions. Despite being a pretty complex topic (IPC) Android and the Binder do a really good job of making it pretty dead simple (at least to get started, I'm sure you could make it complicated if you wanted to ;-) )

    Edit As pointed out in the comments, this is unnecessary if the Service and the client are running in the same process. Unless you specify otherwise, this is the default. However, it still works regardless, it just adds a bit more complexity.

提交回复
热议问题