0

I have a question regarding the performance and solidity at my current project. My Service is using sendBroadcast() to inform the activity (BroadcastReceiver) about some events. For calling functions from the activity defined in the service i create an intent and add data with putExtra. After that i call startService(intent). In the service method onStartCommand i catch the intent data and call depending on the content of the data an internal service function. So every time i want to interact with the service i call startService(intent). Does this way of communication have some disadvantages? Especially if i call startservice very often?

I've also tested successful the communication with Messenger but with this approach i cant call non-static service methods.

Carlo-Rodriguez
  • 158
  • 1
  • 1
  • 8
  • read about `AIDL` and "bound services" – pskink Apr 23 '16 at 18:09
  • i already read this [link](http://developer.android.com/guide/components/bound-services.html). Android recommend to use Messenger or AIDL. With Messenger you cant call non-static methods in service [link](http://stackoverflow.com/questions/11407943/this-handler-class-should-be-static-or-leaks-might-occur-incominghandler). My project is working as expected the question is does my aproach affect the performance or the solidity. – Carlo-Rodriguez Apr 23 '16 at 18:21
  • Yes, the performance will suffer, use either AIDL or Messenger, yes, you can use Messenger – pskink Apr 23 '16 at 18:38
  • Hey, i just tested my code with Messenger and WeakReference to the outer class ([link](http://stackoverflow.com/questions/11407943/this-handler-class-should-be-static-or-leaks-might-occur-incominghandler)). Now i can call a non-static internal service function with service.myFct()....seems to work. Thx pskink for your help. I hope this is more closer to best practice – Carlo-Rodriguez Apr 23 '16 at 19:18
  • you dont even have to use `WeakReference`, you can easily use non-static Handler as you never call `postDelayed` or `sendMessageDelayed` (or any other "delayed" methods) on it, and of course you can use `AIDL` as it is easier to use than sending `Message`s – pskink Apr 23 '16 at 19:22
  • and using `Messenger` is not faster than `AIDL` as both methods use a `Binder` for inter process communication – pskink Apr 24 '16 at 13:34

0 Answers0