我想问一些例子,其中两个不同的活动(第一个活动中的按钮打开第二个活动),正在与一个服务(AIDL等)进行通信我尝试了很多不同的教程,但它们只是关于如何制作一项活动 - >一项服务。
This is probably old, but I'll try to answer it anyways...
In Android, seeing as only one Activity
can bind to a Service
at a time, and only one Activity
can be shown at a time, there is no real reason to want to bind 2 Activities at a time.
But, if you'd like, the best solution is to bind the Service
in the onResume()
method, and unbind it in the onPause()
method. This allows you to give two un-related Activities access to the service, while only having one bound at a time.
Each Activity
is responsible for binding and unbinding from the Service
. This is normally done in onResume
/ onPause
, or onStart
/ onStop
, depending on your requirements. One Activity
cannot bind another Activity
to a Service
. That's just the way it is. :)
You can do it by using Messenger that provide IPC communication without using AIDL. This is how you can bind multiple activities to a service.
If you need your service to communicate with remote processes, then you can use a Messenger to provide the interface for your service. This technique allows you to perform interprocess communication (IPC) without the need to use AIDL.
Have a look at this link . When you see the code, you will find a switch case withing a Handler, this will cater to the multiple requests that you will send from you multiple activities/components. Thanks. I hope the answer will be of help to you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.