Driving a service API from a BroadcastReceiver - android

I get an error message when I attempt to bind from the onReceive() of a receiver to a local service before I drive a bespoke API on it.
"IntentReceiver components are not allowed to bind to services"
What is the best way of getting a piece of data from a service while in a broadcast reciever.
Many thanks.
P.S. I need to get the answer synchronously i.e. I have wait on the answer from the service so a callback may not be possible.

What is the best way of getting a piece of data from a service while in a broadcast reciever.
If the BroadcastReceiver is created from something else (e.g., an activity) and set up with registerReceiver(), then the "something else" is what should be binding to the service and doing the work.
If the BroadcastReceiver is a component registered in the manifest, then you need to rethink your approach to whatever problem you are trying to solve. These sorts of BroadcastReceivers cannot bind to services and cannot spend much time doing work. If the BroadcastReceiver cannot do its work in less than, say, 10ms, it should delegate control to a service via startService(). That service can then do the work on a background thread.
P.S. I need to get the answer synchronously i.e. I have wait on the answer from the service
See above.

Related

Android Bound Service vs Started Service for music app

I am trying to dive deep into service architecture.
Just for testing purpose I am creating music app.
Without doubts music should be played in service, but what kind of communication to use ?
As far as I know service can be bound or started (or both at the same time)
Firstly, I need to play/stop/pause/set source...
Secondly, I need to notify UI if music player is opened about events like progress, buffering...
Here my thoughts about this.
I think about mixing bound and started service.
As far as song can be set only from UI as well as paused/played/stopped/seek I choose communication with service from UI using binder. (Bound service inside activity and get binder back to communicate with service)
Notifications like track completed, next track , current track position coming from the service. I decided to use Broadcast receiver to send such notifications, because it can be multiple interested components.
So my question are
What is the best choice of communication (bound or broadcast) for music player service ?
How does System knows what kind of service is ? I mean that even if the service is bound I need to call startService method at first and than bind it. Does it mean that if even one bindService method was called it is considered as bound service and it would be killed when unbind method is called ?
Does LocalBroadcastManager make sense in communication with service ? As far as LocalBroadcastManager is local for each app, why not to use bound service than ? Global broadcast makes sense in case other apps are interested in events.
Please help to understand this mechanism.
Best choice of communication is using both LocalBroadcasts as well as Binder methods as per your requirement. If you want to do something in service from your bound component like activity then you should use binders. If you need to send result back to application you should use LocalBroadcast.
There is one more option available to use messaging. In this case both activity and service use same ibinder hence two way communication is easy. You can send message from activity to service and service to your activity
In this point you are wrong about starting bound service. You don't need to call startservice in this case. Just a call of bindservice method is required.
bindService(new Intent(this, MessengerService.class), mConnection,
Context.BIND_AUTO_CREATE);
you can bind multiple components to this service. When all of them are unbound then service will be stopped.
Yes LocalBroadcastmanagers make sense. Lets say you want to broadcast something in your app. There are 4-5 components that you want to update.How will you achieve this. Hence the use of localbroadcasts is a good feature.
For example lets say there are two services starting from different activities and second service starts its work when first service has done its work. So, only way to achieve this is send a broadcast and second service will be registering for this broadcast. Hence it will receive it.
Yeah. Services are pretty hard to understand.
There are some things that are easy. A bound service is always started with a bindService method. A started service is always started with a startService method. You do not need to start a bound service, or bind a started service.
A lot of what follows is gross generalization...
Started Service
A started service is nearly useless, unless it is an IntentService. IntentServices are pretty good tools for running asynchronous tasks. You send them a small bundle of parameters and they go off and do whatever those parameters indicate. They are like void methods in that one expects to use their side-effects, not a return value.
Bound Service
A bound service is harder to explain. Although the metaphor breaks down on careful examination, a bound service something like a singleton factory. It is, for instance, a way of providing a single object, with is single state, to all of the Activities in a application. Among its interesting features is that, as long as the service that provided the singleton object is bound, the hosting process is less likely to be killed off. Note that, the "singleton" object that the bound service provides has little to do with the service that provides it. Unbinding the service does not invalidate it.
Bound services are also the main means of doing inter-process communication, in Android.
What should you do?
Well, that's a pretty general question. Here's a thought. Putting your music player in a service makes a lot of sense. If the communication to it is mostly one way -- commands to the service -- there is a chance that you can do it with an IntentService. Until there's a specific reason to do something more complex, an IntentService has the advantage of being simpler.

What's the best way to make a request from an android service to an activity or service and wait for a response?

I've looked at all the IPC mechanisms in android and none seem to do what quite I'm looking for, perhaps there's a better way...
I have a service that's starting up, and it needs to get information from another service or activity in an entirely different 2nd application.
startActivityForResult is what I'm looking for, but that's not available from a service, only an activity.
I'd really prefer to get the information from the 2nd app's activity, but I'm happy to get it from the 2nd app's service.
But the part I'm really interested in, is making the synchronous call from the service and have it wait for a response.
I can send a broadcast intent from the service to the 2nd app's service, but then there's no tie back when the 2nd app's service sends a broadcast intent back with the answer I want.
Is there any way to do something like this from a service?
Is there any way to do something like this from a service?
The calls made to a Binder from a bound service are synchronous. The documentation discusses bound services, with a separate page on the AIDL you will need since the services are in different applications.
Personally, I would rewrite the first service to not need the synchronous call.

Start Again Already Running Service?

I have a broadcast receiver listening called PACKAGE_ADDED and an other broadcast receiver listening called BOOT_COMPLETED. The bootcompleted broadcast receiver starts my service.When the new app is installed, I want to send a message to my service .The first solution that came to my mind was to start the service again with
intent.setAction("NEW_APP_INSTALLED");
startService(intent);
without stopping the service and check the intent.getAction() value in theservice.onStart() method. If the result is NEW_APP_INSTALLED, then call newAppInstalled().I don't think this is an elegant solution.
Is starting the service repeatedly a problem? And what happens when my activity binds to it via ipc(aidl) while fetching data and the new app installed broadcast receiver starts it again? Lastly what is the best way to solve my problem?
Is starting the service again and again problem?
Lastly what is the best way to solve my problem?
It is perfectly fine for you to call startService on an already running service. And you can either do it the way you suggested or have two different services (one for boot, one for new_app) or you can register a BroadcastReceiver in the service after it's started, but that wouldn't be effective because then if you try to send a message to it and it's not running already, it won't get the message.. I prefer one service as you suggested and using startService.
And what happens when my activity bind it via ipc (AIDL) fetching data
and new app installed broadcast receiver starts it again?
Well, I don't know anything about AIDL, really. This might help. That page does state "Most applications should not use AIDL to create a bound service". This is because it makes multi-threading needed and makes it more complicated.
Please let me know if I failed to answer to your satisfaction - though I can't really elaborate on AIDL specifically because I don't know anything about it.

Broadcast on service start?

Does android send any kind of broadcast when a new service is started/restarted.
underlying reason is i'd like to catch whenever a background service is starting without my knowledge.
any answer to how it's possible, if it is, are appreciated.
EDIT:
I want it to work with all services, not only my own services.
If the background service is in your control then you can use onBind() method to get to know that the service is started by bindService() and same time you can use onStartCommand() method if the service is started by startService()....
That mean you want to access system service or what? or service which you have written in your android application?
if you want to control your service which you have written in your application then as above told you can control it..or if you want to start or stop service when you want then you can create broadcast receiver and write onreceive() what you want to do.

starting an asynctask from a broadcast receiver

I want to know if starting up a asynctask from a broadcast receiver considered a bad practice? I basically registered with the C2DM server of google and then when I intercept the onregistered, broadcast receiver, I want to send it to my server.
what is the best way of accomplishing this?
Yes, this is considered bad practice. That's because if you start AsyncTask from BroadcastReceiver Android may kill your process if onReceive() returned and there is no other active components running.
The correct way would be to start Service from BroadcastReceiver. And this Service should manage AsyncTask. This way Android will be aware about the active component and Android will not kill it prematurely (unless other critical conditions arise, like not enough memory conditions).
Starting with Honeycomb, you can call goAsync(), and then you have 10 seconds or so to do your work asynchronously .
Example of usage can be shown here.

Categories

Resources