I have an android class that extends a service. I can start the service with startService(intent) and stop it with stopService(intent).
But what if I need send some information to the service after I've started it?
In my case for example I need to call some methods. How can I do it?
I thought is a simple thing but I looked for it on the web without find an easy way. Do I need to use a remote service or there is something easier?
Thank in advance
or just use startService() and send an Intent with some data along. No need to be complex.
Use Broadcasts and Broadcast Receivers.
To call methods in the Service from an Activity you need to bind the Service in an Activity. Check out Bound services from the official developers guide.
If you don't want to use an Activity then resort to Nieks suggestion with Broadcasts and Broadcast Receivers.
But then again if you need to call methods in you service on a regular basis, maybe it shouldn't be a service.
Convert your service over to an IntentService and it will handel the service life cycle for you.
Each startService(intent) will represent a new "job" for the service.
If you need notification of progress or completion Use Broadcasts and Broadcast Receivers.
Related
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.
I'm developing a service and I've been following an example that unfortunately uses an Activity to get its work done. The example uses startActivityForResult() to get values after doing something. I'm wondering do I just use BroadcastReceiver to accomplish the same thing? sendBroadcast(intent) and then capture the broadcast to do whatever?
Yes, for me is the best solution, but you can bind the service to the activity if you want another solution.
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.
Hope someone can help me out here. I will try to be concise!
I have a widget which starts a service. The service registers two broadcast receivers. I would like to send back intents from the receivers to the service, so that the service can react.
I believe I read somewhere that 'starting' the service multiple times works, e.g. do the following in the receivers:
serviceIntent.setAction("me.SERVICE");
intent.putExtra("me.SERVICE", somedata);
context.startService(serviceIntent);
I remember reading (on some blog) that this won't start a new service, but will simply pass the intent to the already running service. Is this correct? Is it a bad way of doing it? Is there a better way?
Thanks very much!
Jack
Yes, I've used that approach in a pre-2.0 app.
I accomplished this using a singleton. I set a private variable (pointing to 'this') in the service's onCreate, and then used a static method called getInstance() which would return it. So later on, I simply call MyService.getInstance() to get hold of the service.
I have built an application that listen to call state changes, and I want to notify a service when the call_state became IDLE.
All the components I have are functional, I just need to notify (not start) a service for this.
What's the correct practice, maybe using AIDL?
Because, in a PhoneStateListener, I can't bind to a service. Do I have to start an activity for that?
I'd think you'd be better off sending your service a broadcast intent than trying to bind to it.
When you want to notify your service you will need to call though to its process via AIDL.