Services and Broadcast Receivers

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Services and Broadcast Receivers

EaranMaleasi
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 for how to setup an service and an broadcast receiver, i would be deeply grateful.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Services and Broadcast Receivers

ledz
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


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.

Hope this can be of any help.



On 16 November 2012 07:52, EaranMaleasi <[hidden email]> 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
[hidden email]

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



--
Gonçalo Oliveira

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Services and Broadcast Receivers

EaranMaleasi
thank you for your peace of code, and sorry that i'm posting back only now.

i am figuring out at the moment how it works(yeah do not only C&P even if i can't really understand it atm.) and how i implement it.

i have a problem with this one:
SafeUnregisterReceiver

this method seems to be nowhere in all the android resources and is nowhere to be found in the documentation...or am i blind?

Ps: IntentService:
Am i right that this service starts, when you call it, starts an worker threat and stops it when the threat is finished? would that mean it can't store any data?

i want the service to store data from an Database, so i do not have to do SQL queries all the time.
from this temporary storage i want to call just an singe cursor (i have 4 or 5) from an activity
the called data will be manipulated and send back by that activty, which can happen more than once with every cursor.
at the end of the service all data should be writen back in the DB by altering...deleting...or inserting entries.
this will be not to hard, (at least i think it will be), and i hope i wont need help with that,
just want you to tell what i plan to do... but you could tell me if this idea of managing data is crap or not,
wheter i should do sth like that, or just do all the stuff direcly in the DB (which would be less programming)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Services and Broadcast Receivers

ledz
My bad with the SafeUnregisterReceiver, it's an extension. here's the code...

        public static void SafeUnregisterReceiver( this ContextWrapper ctx, BroadcastReceiver receiver )
        {
            if ( ( ctx != null ) && ( receiver != null ) )
            {
                ctx.UnregisterReceiver( receiver );
                receiver.Dispose();

                receiver = null;
            }
        }



On 20 November 2012 08:41, EaranMaleasi <[hidden email]> wrote:
thank you for your peace of code, and sorry that i'm posting back only now.

i am figuring out at the moment how it works(yeah do not only C&P even if i
can't really understand it atm.) and how i implement it.

i have a problem with this one:
SafeUnregisterReceiver

this method seems to be nowhere in all the android resources and is nowhere
to be found in the documentation...or am i blind?

Ps: IntentService:
Am i right that this service starts, when you call it, starts an worker
threat and stops it when the threat is finished? would that mean it can't
store any data?

i want the service to store data from an Database, so i do not have to do
SQL queries all the time.
from this temporary storage i want to call just an singe cursor (i have 4 or
5) from an activity
the called data will be manipulated and send back by that activty, which can
happen more than once with every cursor.
at the end of the service all data should be writen back in the DB by
altering...deleting...or inserting entries.
this will be not to hard, (at least i think it will be), and i hope i wont
need help with that,
just want you to tell what i plan to do... but you could tell me if this
idea of managing data is crap or not,
wheter i should do sth like that, or just do all the stuff direcly in the DB
(which would be less programming)



--
View this message in context: http://mono-for-android.1047100.n5.nabble.com/Services-and-Broadcast-Receivers-tp5712394p5712430.html
Sent from the Mono for Android mailing list archive at Nabble.com.
_______________________________________________
Monodroid mailing list
[hidden email]

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



--
Gonçalo Oliveira

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Services and Broadcast Receivers

Sait
In reply to this post by ledz
sir , is there a method called register? there is registerreceiver  i don see register method or maybe be i dont no it
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Services and Broadcast Receivers

johnbowick
In reply to this post by EaranMaleasi
Service: If you want to do something in background , this will be running always in background even if the application closed. You can create this in separate process and also you can give your service to other app if you want. Downloading any content or Music is good example

Broadcast Reciever: Usually system will send some info which can be recieved by your app if you would wish to ,by registering. And you can do something what you want when that thing happens by using onReceive method.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Services and Broadcast Receivers

CDrosos
This post has NOT been accepted by the mailing list yet.
In reply to this post by ledz
Hello guys, im trying to understand what a broadcast reciever is and how it works. in this example i cant understand what is AppAction.TaskStatusChanged and why i need it and also the [BroadcastReciever] code is missing i think, can someone write a possible code for this example? i only want an example of sending data from a service into a fragment with BroadcastReciever. this looks like a good and simple example
Loading...