This is a mirror of official site: http://jasper-net.blogspot.com/

Android Services and Broadcast Receiver Tutorial

| Sunday, March 13, 2011
Developing services with Android Gingerbread and Eclipse
This tutorial describes how to create and consume Android services. It is based on Eclipse 3.6, Java 1.6 and Android 2.3 (Gingerbread).

1. Android Services

1.1. Service

A service is a component which runs in the background, without interacting with the user.

The Android platform provides a lot of pre-defined services, usually exposed via a Manager class. In your activity you access services via the method getSystemService().

Own Services must be declared via the "AndroidManifest.xml". They run the main thread of their hosting process. Therefore you should run performance intensive tasks in the background .

1.2. Own Services

You can declare your own service to perform long running operations without user interaction or to supply functionality to other applications. A activity can start a service via the startService() method and stop the service via the stopService() method. If the activity want to interact with the service it can use the bindService() method of the service. This requires an "ServiceConnection" object which allows to connect to the service and which return a IBinder object. This IBinder object can be used by the activity to communicate with the service.

Once a service is started its onCreate() method is called. Afterwards the onStartCommand() is called with the Intent data provided by the activity. startService also allows to provide a flag with determines the lifecycle behavior of the services. START_STICKY is used for services which are explicit started or stopped. Services started with START_NOT_STICKY will end automatically after the onStartCommand() method is done. A service is started within the main thread of the application therefore all long running tasks should be performed in the background .

A Services needs to be declared in the "AndroidManifest.xml" via a <service android:name="yourclasss"> </service> and the implementing class must extend the class "Service" or one of its subclasses.

One common case is that the service and the activity are very closely related. In this case class just case the IBinder to the class the service provides. See Local Service Example for an example.

Another example would be the usage of Remote Messenger Service. In this case activities can send messages to the service and receive messages as well if they register for them.

1.3. Broadcast Receiver

A broadcast receiver is a class which extends "BroadcastReceiver" and which is registered as a receiver in an Android Application via the AndroidManifest.xml (or via code). This class will be able to receive intents via the sendBroadcast() method. "BroadCastReceiver" defines the method "onReceive()". Only during this method your broadcast receiver object will be valid, afterwards the Android system will consider your object as no longer active. Therefore you cannot perform any asynchronous operation.

1.4. Pending Intent

This tutorial will also use a PendingIntent. A PendingIntent is a token that you give to another application (e.g. Notification Manager, Alarm Manager or other 3rd party applications), which allows this other application to use the permissions of your application to execute a predefined piece of code.

Read more: vogella.de

Posted via email from Jasper-net

12 comments:

Anonymous said...

It's an amazing paragraph in support of all the web users; they will take benefit from it I am sure.

My web page :: diets that work for women

Anonymous said...

Whеn yߋu աish too obtain tҺe software, visit tҺe Android оs Market ρlace
oor еven Apple inc Applicatiokn Merchant аոd look
fоr the bingo. Generɑlly tҺere'azines an еntirely globe aѵailable.

Independent tо thе Ps3 slim bundle, Play - Տtop joined ѡith theѕe people aat Rockstar Video games to generate ɑ custom mаde-designed GTA 4 cheats Versus Heart beat
wireless headset, іn order to Һave fun աith the video game'ersus
mɑny specific r / c aոd alkso in-video game soundtrack alongside superior ѕeem.



Check оut my sitte cheats gta 4

Anonymous said...

Insert one end of its power supply into the device's
"Power" or "AC" port. It features more than 100 Mbps of data transfer
in both upstream and downstream since it makes use
of DOCSIS 3. Parallel data pipes are able to act as a single broadband conduit
as a result of the channel bonding.

Here is my webpage - http://www.youtube.com/watch?v=4kR5deJViPs

Anonymous said...

For hottest news you Һave to visit internet and on web I found tɦіs web pаge
as ɑ moѕt excellent web site for hottest updates.

Visit mʏ webpage: Newspaper Obituaries

Anonymous said...

This is very interesting, You are an overly professional blogger.
I've joined your rss feed and look forward to looking for more
of your great post. Also, I have shared your website in my
social networks

my web page :: www.quincychurch.org

Anonymous said...

Hi, I do think this is a great website. I stumbledupon it ;) I
will return once again since I book-marked it. Money and freedom is the greatest way to change, may
you be rich and continue to guide others.

my web page: garden sheds plastic

Anonymous said...

Do you mijd if I qiote a couple of your articles as long
as I provide crerdit and sources back to your weblog? My blog
site is in the vwry same area oof interest as yours
and my users would definitely benefit from spme
of the information youu progide here. Pleas let me knhow if this okay with you.
Many thanks!

Stop by my web blog ... buy youtube iews reviews - -

Anonymous said...

I am sure this article has touched all the internet visitors, its really really good post
on building upp new website.

My webpage :: buy youtube likes and views

Anonymous said...

I'νe besn pursuіng your blog ѕҟte for a ԝhile, and can I say your artiϲles is improѵing each and every time I
rеad your new pоsts. I ove your manner of article wrіting aas welll
as the ѡаy you submit the info, which is really clean and accurate which makes
for an extremely easy reаd.Thɑnk you foor your wonderful words!


Visit my web pagbe ... E liquids

Anonymous said...

I am not sure where you're getting your information, but great topic.

I needs to spenmd some time learning more or understanding more.
Thanks for great information I was looking for this information for
my mission.

Also visit my web page :: buy youtube subscribers cheap

Anonymous said...

Il corpo è composto quasi interamente di plastica e la qualità costruttiva
è abbastanza buona. LG P350 è strettamente legata al social networking,
come esso integra molte funzionalità di Google attraverso l'androide
system. Connettività: Questa prossima generazione i - Pad contribuisce a mantenere collegato mentre travelling.


Also visit my weblog ... Trucchi 2048 Iphone (weebly.com)

Anonymous said...

What's up, its fastidious paragraph about media print, we all be aware of media is a enormous source of data.


Also visit my blog rodents removing