I have an activity that creates an intent, puts some extras with putExtra() and calls the startService(intent) to start a service.
This service calculates some stuff based on the extras and then I want to send the result back to the activity.
In which way can I do this?
I tried to create an intent on my service and broadcast it using sendBroadcast(). I have a broadcastReceiver on the activity but Im not sure if I register it correctly. Im confused!
Is there any other way to do so? Something like StartActivityForResult but for services (something like StartServiceForResult or something)?
The method you are attempting to use is good! Without code though it will be hard to say what you might be doing incorrectly.
In your service you would broadcast an intent like this...
then in your activity you could have something like...
Create an action string:
In your onResume() method you would have...
Hope that helps
You can do these three things which I know:
Note: You can also use DB which is good if u have bulk of data.
First, you can have the service send the intent directly to the activity. That way, even if the activity isn't currently running, it will be launched.
Second, you start the activity with
FLAG_ACTIVITY_SINGLE_TOP
. This keeps the activity from launching if it's already running.Third, you implement
onNewIntent()
in your activity to catch any intents the service sends if your activity is already running.