I got a bit bothered with the need to have to create a broadcast receiver
for every little thing, so I created a generic broadcast receiver with a
delegate. It simplifies the process...
You can grab the source here

http://pastebin.com/yBa5LP6C

The way to use it is very simple.
>From the service, send a broadcast with the data you need

Intent broadcastIntent = new Intent( some_action_identifier_here );
broadcastIntent.PutExtra( "some_var", "some_string_value" );

SendBroadcast( new Intent( AppAction.TaskStatusChanged ) );

In the activities that you want to receive the notifications, you'll have
to register the receiver. A good practive is to register on OnStart and
unregister on OnStop.

        protected override void OnStart()
        {
            base.OnStart();

            myReceiver = SimpleBroadcastReceiver.Register(
MyCallbackMethod, this, the_same_action_identifier_here );
        }

        protected override void OnStop()
        {
            base.OnStop();

            SimpleBroadcastReceiver.Unregister( this,myReceiver );
        }

        private void MyCallbackMethod( Context context, Intent intent )
        {
            var valueFromIntent = intent.Extras.GetString( "some_var" );
            // do something
        }

Also, if you don't need the broadcasts to be delivered outside your
process, use local intents instead. The usage is pretty much the same and
you'll add extra security to your app.
To receive local intents with the SimpleBroadcastReceiver use the methods
RegisterLocal and UnregisterLocal instead. And to send local intents use
the LocalBroadcastManager.

Also, for your purposes, you might want to create an IntentService instead.
http://developer.android.com/reference/android/app/IntentService.html

Hope this can be of any help.



On 16 November 2012 07:52, EaranMaleasi <d.v...@weitzmann.com> wrote:

> hey guys,
>
> i want to setup an Service, which is responsible for all Data from and into
> the Database.
> because of the point, that i want to get Data from the Service to my
> Activities,
> i've searched for sth. that can send Data to an activity without starting
> it
> like a "simple" intent with extras,
> and the startActivity() method.
> so i stumbled upon the Broadcasts and read the documentation and looked
> through the sampels,
> but at the moment it's just to complex for me how to setup these.
>
> if you have an better example for me, then  this
> <
> http://docs.xamarin.com/Android/Guides/Application_Fundamentals/Services/Part_1_-_Started_Services
> >
> for how to setup an service and an broadcast receiver, i would be deeply
> grateful.
>
>
>
> --
> View this message in context:
> http://mono-for-android.1047100.n5.nabble.com/Services-and-Broadcast-Receivers-tp5712394.html
> Sent from the Mono for Android mailing list archive at Nabble.com.
> _______________________________________________
> Monodroid mailing list
> Monodroid@lists.ximian.com
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid
>



-- 
Gonçalo Oliveira
_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to