I've read the documentation about Bound Services, where it is shown that you can easily communicate through Messages from an Activity to a remote (i.e. not in the same context) Service but is there any way to send messages from the Service to the bound Activity? For example, my activity bounds to a running background service of the same application, sends a message to it and upon the reception of this message the service replies with a message to the activity.. how do I implement this? Can you point me to some documentation that explains this topic?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
1) implement transact/onTransact methods in own Binder.class and binder proxy implementing IInterface.class objects (anon or by extending a class direct) by use of passed in those methods Parcel.class object
2) attach local interface to own Binder object 3) create service and return a binder proxy implementation from onBind method 4) create bond with bindService(ServiceConnection) 5) this will result in returning proxy binder via created bound in interfece implementation
this is an android implementation of IPC with usage of kernel binder space
simplifying in code example :
*then you could use the IBinder on client and service side the transact method to talk with each other (4 example using odd/eve codes to disgusting local remote side as we use same onTransact method for booth sides)
Found example in the reference documentation at Remote Messenger Service Sample.
NOTE: This is only for in-process services and activities, not remote like question asked.
Using a service to communicate with an activity involves making a listener that you can pass to the service from the activity.
You need to make a service that is bound to an activity.
The first step is making a service. In the service make sure you have a Binder object and the method to return a binder object. Below is an example that I used in my service to retrieve my binder. Also notice this binder has a method to set a listener, which will be saved in the service as a BoundServiceListener type field.
Now you need to create some kind of interface that you can pass to the binder object that your service can use to send updates to. Below is my BoundServiceListener.
Now in your activity you need to create a ServiceConnection object that is used for binding to a service. SO add somethinglike this.
Now the key line to notice here is
This part is where I am actually sending my BoundServiceListener interface to the service class. The service class then uses that listener object here
Now you can put this anywhere you need to in your service class, and your activity will receive your progress update.
Also be sure to include following in your activity to actually bind and start the service.
Keep in mind that even though you bind to a service, it it not actually started until you call start service. Binding to the service just connects the service to an activity. the startService() method calls the services
Also declare your service in your manifest
Also unbind the service when the activity leaves by
Hope this helps.