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.
Related
I am writing here after countless hours searching the net with no gain.
I am trying to create an app which starts a service, this service will have a broadcast receiver in him which should detect when a phone call is received, than this receiver calls a method inside the service with the calling number.
Can anyone help me out here? what should i be looking for? in short how should i handle this?
Check out this question. Sounds like for you, you might simply use a singlton instance of your service, or perhaps a broadcast receiver. Definitely stay away from AIDL.
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.
Alright, So I have an application that receives an intent in a Broadcast Receiver, but what I need to do with it, I cant do without extending Activity, so I launch an activity from that receiver, but how can I get it to do what I want it to do, without starting any UI of any kind? Thanks!
Use a Service
I'm trying to start a Service from my Activity to look out for changes on a web page, it's a private app so I don't bother the battery life...
But I'd like to pass data from my Service to my Activity... I can't seem to find a way to call the Activity from my Service. How can I achieve this?
As Alex indicated, you can bind to the service and pass some sort of listener or callback to the service to use on events.
Or, you can use a broadcast Intent, perhaps using methods like setPackage() on the Intent to limit the scope of the broadcast.
Or, you can use createPendingResult() to create a PendingIntent that you pass as an Intent extra to the service -- the service can then use that PendingIntent to trigger onActivityResult() in your activity.
Or, you can use a ResultReceiver.
Or, you can use a Messenger.
(admittedly, I have not tried those latter two approaches, but I think they will work here)
One more alternative: if your service updates content provider, activity can be notified via ContentObserver. This would be enough if your service downloads some data from server and you simply want to display fresh contents in the activity.
Some ugly ways:
1.) If the activity has not started yet, then use intent and startActivity, but remember intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2.) Otherwise if the activity has started already, you can write your own callback method in the activity and register the method in the service, then direct call the method in the service.
Hope to find some smart way.
I think broadcast also work well, you can write a static inner class for receive broadcast and start activity. But it is also ugly in my opinion.
The ResultReceiver mechanism has been explained in another post :-
Restful API service
However it would not work in all cases. Please refer to my comment on that post. The limited scope broadcast or PendingIntent mechanism seem more suitable.
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.