Hi,
If you create the BroadcastReceiver as an inner class, you do have
access in the broadcastreceiver to anything in your activity.
So in you activity, do something like:
/**
* Receive messages from our Service
*/
private BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String data = intent.getStringExtra("testdata");
iAmAMethodOfTheContainingActivity(data);
}
};
protected void onResume() {
super.onResume();
// register as a BroadcastReceiver for Intent action from our
Service
registerReceiver(receiver, new IntentFilter
(MyService.SOME_ACTION));
}
public void iAmAMethodOfTheContainingActivity(String data)
{
System.out.println("Received: " + data);
}
And in your service, something like:
Intent intent = new Intent(SOME_ACTION);
intent.putExtra("testdata", "Some result");
sendBroadcast(intent);
Note that communication in the other direction (calling the service
from the activity) can be done in a similar way, using startService
from the activity, and passing the arguments through the Intent.
hth,
Wouter
On Mon, Feb 23, 2009 at 10:26 AM, Marius Shekov <[email protected]>
wrote:
Hey Wouter,
Thanks for your reply. However, I am not really able to understand
what you mean with "If you then use a BroadcastReceiver IN the calling
activities". The only thing I know is how to create (separate)
BroadcastReceivers (they are in a separate .java file, a separate
class, which must be this way, because in Java you can only use
"extends" for one class, there's no multiple inheritance) and how to
register them (using the xml file for example) and how to receive
intents with them. However, once I have the intent in the broadcast
receivers onReceive() method, it's just as useless as I do not know
how to inform my activity about that. Something like
context.startActivity(i) in onReceive() will not work, unless I set
i.setFlags(FLAG_ACTIVITY_NEW_TASK) which I do absolutely not want (I
want to inform an already existing activity and bring its stack to the
front).
I hope that you could clarify what you meant in your email.
Greetings
NameZero912
-------- Original-Nachricht --------
> Datum: Sun, 22 Feb 2009 15:48:25 -0800 (PST)
> Von: Wouter <[email protected]>
> An: NameZero912 <[email protected]>
> Betreff: Re: Local service: how to tell activity when done?
> In your service, you can do 'sendBroadcast(Intent)', adding the
result-
> data
> as Extra in the intent.
> If you then use a BroadcastReceiver in the calling activities
(create,
> override
> onReceive, and register with 'registerReceiver'), it gets
notified of
> the broadcast,
> and receives the data.
>
> hth,
>
> Wouter
>
> On Feb 22, 1:44 pm, NameZero912 <[email protected]> wrote:
> > Hi there,
> >
> > starting from the example ServiceStartArguments.java of the
"API
> > demos", the service "ServiceStartArguments" is creating a new
Handler
> > object (ServiceHandler) which does all the work in a separate
thread.
> > Once that work is completed, how can I tell other components
that my
> > work is finished, and also provide data (that resulted from
the work)
> > to them?
> >
> > I tried something like startActivity
(intent_transferred_via_msg) which
> > does not work, I'll get an exception because I call it
"outside of the
> > service", which is technically true.
> >
> > Greetings
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---