Sunteți pe pagina 1din 13

15/07/2015

Android Fundamentals and Components | Techblogon

Home
About us
Contact us
Privacy Policy

Tutorials
Gadgets
Internet
Technologies

Android Fundamentals and Components


January 27, 2013 Android Tutorial, Tutorials
Android Fundamentals and Components
Before going to start development, we need to know about Android Fundamentals and Components, which we
will use frequently while developing our android applications. From a developers perspective, some important
building blocks / components of android are as bellow. Just have a look at them for now, we will discuss them in
details latter on.
1. Activities
2. Services
3. Broadcast Receivers
4. Content Providers
When we are developing application, always we will have some requirements like, passing messages within an
application (one screen to another) or between different applications.
For example: Lets say a new message came to your phone, so we update the notification bar for that new
message. So we will have to pass the information from message application to notification bar. Moral of the story
is, the means of communication between the above mentioned components is through the bellow components.
1.Intents

http://techblogon.com/android-fundamentals-components-activity-service-broadcast-receiver-content-provider/

1/13

15/07/2015

Android Fundamentals and Components | Techblogon

2. Intent Filters
Lets come to the GUI part. The User Interface elements are made up the below components, which we called:
1. Views
2. Notifications
Now it is time to describe in details for each of the above components and its requirement and usages.

Android Activity
Activity is the basic building block of every visible android application. It provides the means to render a GUI.
Every screen in an application is an activity by itself. We can call each visible component as an activity in
android. Though more than one activities work together to present an application sequence, each activity is an
independent entity. Just have a look at the below image, which explains the life cycle of an activity. I will explain
the practical use of an activity, when we will develop our first Hello World application in this tutorial.
Life Cycle of an Activity in Android

http://techblogon.com/android-fundamentals-components-activity-service-broadcast-receiver-content-provider/

2/13

15/07/2015

Android Fundamentals and Components | Techblogon

To make you understand the real usage click here for complete example.

Android Services
Service is another building block of android applications which does not provide any UI. It is a program that can
run in the background for an indefinite period. That means if we want to do a long operation (example: download
data from internet), then we need to create an Android service for this purpose.
Android Services are little bit confusing. Before going to know What is Android Service? Lets have a clear
http://techblogon.com/android-fundamentals-components-activity-service-broadcast-receiver-content-provider/

3/13

15/07/2015

Android Fundamentals and Components | Techblogon

picture in our mind that, What Android Service is not?


Please keep in mind that, Android Service is neither a separate process nor a thread. Then what exactly it is?

Using Android Service, we just inform the Android OS that, We are going to do a background
processing.
Also Android Service exposes some functionality to other applications using the bind mechanism.
Generally Android Service are of 2 types.
Started Service (Unbounded)
This type of service is created and called by Android Activities. There is no 2 way communication between
Android Activity and Service. The Activity just starts the service and does not care about the status of the
service. The Service will finish its work and automatically stops when finish its job.
Bound Service (Bounded)
This type of Android Service is for 2 way communication. Suppose an Android Activity has started a bounded
service, then Activity can be notified about the status by the service.
Dont worry we will discuss in details about Android Services with an example in the example section.
LifeCycle of a Service in Android

http://techblogon.com/android-fundamentals-components-activity-service-broadcast-receiver-content-provider/

4/13

15/07/2015

Android Fundamentals and Components | Techblogon

To make you understand the real usage click here for complete example.

Android Broadcast Receivers


Broadcast Receiver is yet another type of component that can receive and respond to any broadcast
announcements.
Lets take an example; when a new message comes to your inbox, this information will be broadcasted for other
applications. If any application wants to do something while a new message comes to your inbox, then it can
receive the broadcasted message details (Like: senders number, content etc.) and process accordingly. Your
application must be registered to that receiver to receive the broadcasted message.

Android Content Providers


Content Providers are a separate league of components that exposes a specific set of data to applications. Lets
take an example: If you want to search a contact in your contact database (Like: name, number etc), then you
can use Content Provider for this purpose. You can say Content provider is the pointer in your application which
points to a specific database from other application.

http://techblogon.com/android-fundamentals-components-activity-service-broadcast-receiver-content-provider/

5/13

15/07/2015

Android Fundamentals and Components | Techblogon

I think the basic knowledge of these four components is good enough to start development now, the knowledge
of the means of communication between the components is also essential. So the android platform offers a new
concept of communication through intents and intent filters.
To make you understand the real usage click here for complete example.

Android Intents
Intents are messages that are passed between components. So we have a question in our mind. Is it equivalent to
parameters passed to API calls? Yes, it is close to that. However, the fundamental differences between API calls
and intents is the way of invoking it. Lets have a look at the difference between these two in below points.
1. API calls are synchronous while intent-based invocation is asynchronous (mostly)
2. API calls are bound at compile time while intent-based calls are run-time bound (mostly)
It is these two differences that take Android platform to a different league.
In simple word, the core android components of an application activities, services, and broadcast receivers
are activated through messages, called intents.
For example an activity can send an intent to the Android system which starts another activity. So an Intent is
just a way to send a message in android.
Android intents are basically 2 types in nature.
1. Implicit Intents
Implicit intents specify the action which should be performed by other components or applications.
http://techblogon.com/android-fundamentals-components-activity-service-broadcast-receiver-content-provider/

6/13

15/07/2015

Android Fundamentals and Components | Techblogon

For Example: If you want to open an URL in a web browser from your application code, Then following code
tells the Android system how to view a webpage. Typically the web browser is registered to this Intentbut
other component could also register themselfs to this intent. That means if you have installed web browsers like
IE, Mozilla Firfox and Google Chrome, then all browsers might be registered to the intent
(Intent.ACTION_VIEW) to show a web page as per you request.
1 Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.techblogon.com"));
2 startActivity(i);

If only one component (here web browser in our example) is found, Android starts this component directly. If
several components are identifier by the Android system, the user will get an selection dialog and can decide
which component should be used for that Intent.
2. Explicit Intents
Explicit intents explicitly defines the exact component which should be called by the Android system, by using the
Java class are identifier.
The following code shows how to create an explicit intents and send it to the Android system. That means
Android system will directly execute your intent request as you requested. Explicit intents are typically used
within an application, as the classes in an application are controlled by the application developer. If you want to
open an Android Activity from another activity, then below is the code for it using an intent to do so. Also you
can send some data to that activity if required.
1 Intent i = new Intent(this, ActivityTwo.class);
2 i.putExtra("First Value", "This First Value for ActivityTwo");
3 i.putExtra("Second Value", "This Second Value ActivityTwo");

Android Intent Filters


To inform the system which implicit intents they can handle, activities, services, and broadcast receivers can have
one or more intent filters. Each filter describes a capability of that component, a set of intents that the component
is willing to receive.
Lets take an Example: The NotePadEditor activity of the sample Note Pad application has two filters one
for starting up with a specific note that the user can view or edit, and another for starting with a new, blank note
that the user can fill in and save.
Basically IntentFilters are defined in the AndroidManifest.xml file. For BroadcastReceiver it is possible to define
it in our code itself. An IntentFilters is defined by its category, action and data filters. It can also contain
additional metadata. If a component does not define an Intent filter, then it can only be called by explicit Intents.
To make you understand the real usage click here for complete example.
Previous Page
Android Tutorial Home Page
http://techblogon.com/android-fundamentals-components-activity-service-broadcast-receiver-content-provider/

7/13

15/07/2015

Android Fundamentals and Components | Techblogon

Next Page

Post By SmrutiRanjan (57 Posts)

C ONNECT

Working @ Samsung as a Project Lead for Android


Smartphones. I have been blogging since 2008.
Previously I was writing articles for other bloggers, but
finally I have started my own blog-"Techblogon".I am also
an active contributor for the blog-"Gadgets n Gizmos
World". Job is my necessity, but blogging is my passion.
Website: Techblogon

Activity in android, broadcast receiver in android, content provider in android, intent filter in android, intent in
android, notification in android, Service in android, views in android

15 Responses to Android Fundamentals and Components


1.

Gagandeep Singh says:


March 19, 2013 at 9:48 am
awesome !!!
Reply

2.

Dharmendra kumayu says:


March 26, 2013 at 8:19 am
its really good!
Reply

3.

Abinash says:
March 29, 2013 at 12:22 pm
I am new to android development though i have 4.5 years experience in .net technologies..i find this
man named SmrutiRanjan is really a great guyits really very easy to understand..thank bro.
Cheears
Abinash Routray
Reply

4.

Manish Agarwal says:


April 4, 2013 at 5:47 pm
Hey there.
I am totally new to app development and dont know a thing about API.
Should I first learn something about API to continue with your tutorials?

http://techblogon.com/android-fundamentals-components-activity-service-broadcast-receiver-content-provider/

8/13

15/07/2015

Android Fundamentals and Components | Techblogon

If yes, may you kindly provide to some good articles or tutorials about the same?
Reply
SmrutiRanjan says:
April 5, 2013 at 5:28 am
Dear Manish,
API are just the collection of Android functions, Which has already exist with the default Android
SDK. We just need to call them in our application, so dont worry.
Reply
5.

P.S.Sanatan says:
April 12, 2013 at 7:13 am
i came across your blog and i think you can help me.I dont know about java programming and i have
basic idea about the c and c++.Then what should be the procedure for me to learn the anroid application
development.Please guide me
Thank you
Reply
SmrutiRanjan says:
April 12, 2013 at 10:29 am
Dear Sanatan,
It is not necessary to know java for android.
Just setup all android tool, then just learn some common fundamentals like activity, intent, broadcast
receiver etc. then start will hello world program
You can refer to my site, it is written in such a way that begginers can read from the top to the
bottom, which will help you to go ahead step by step.
Reply
P.S.Sanatan says:
April 16, 2013 at 10:35 am
Thank you for your valuable guidance,i am going to do the same thing as told by you and
hope you will guide in my way of learning anroid.
Thank you.
Reply

6.

Jones sabo newest charms says:


April 14, 2013 at 2:09 pm
Fantastic goods from you, man. I have understand your stuff previous to and youre just too great.

http://techblogon.com/android-fundamentals-components-activity-service-broadcast-receiver-content-provider/

9/13

15/07/2015

Android Fundamentals and Components | Techblogon

Reply
7.

Jones sabo says:


April 27, 2013 at 4:00 pm
Some genuinely excellent blog posts on this website , appreciate it for contribution.
Reply

8.

leela says:
May 18, 2013 at 6:07 am
hai sir good morning iam new to Android technology iam learning basics from ur website its really good
and i want to learn android technology is it good has it future for this technology i want to know please
give guidance for this and also jobs rating and openings for this technology
Reply
SmrutiRanjan says:
May 18, 2013 at 6:13 pm
Very good Android market
Reply

9.

Anil SIngh says:


May 20, 2013 at 4:13 pm
You are great. I just want to confirm is an activity killed when we press back button on our device?
Reply

10.

principally says:
July 24, 2013 at 9:55 pm
Awesome things here. I am very glad to peer your article. Thank you a lot and im having a look forward
to contact you. Will you please drop me a mail?
Reply

11.

Faisal Goraya says:


October 29, 2013 at 5:45 pm
i cannot understand, What is android intents? please explain android intent using simple example?
Reply

Leave a Reply
http://techblogon.com/android-fundamentals-components-activity-service-broadcast-receiver-content-provider/

10/13

15/07/2015

Android Fundamentals and Components | Techblogon

Your email address will not be published. Required fields are marked *
Name *
Email *
Website
3 = two

Comment
You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym
title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del
datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" dataurl=""> <span class="" title="" data-url="">

Post Comment

Search

http://techblogon.com/android-fundamentals-components-activity-service-broadcast-receiver-content-provider/

11/13

15/07/2015

Android Fundamentals and Components | Techblogon

Techblogon
Follow

+1

+ 305
Like 1,859 people like this. Be the first of your friends.

Recommend on Google

Follow @techblogon

216 follow ers

Enter your Email Address...


Subscribe

RSS Feed

Google Plus

Archives
March 2015
July 2014
April 2014
October 2013
August 2013
July 2013
June 2013
May 2013
April 2013
March 2013
February 2013
January 2013
December 2012

Categories
Android
Android Tutorial
Design Patterns
Displays
E-Commerce
Gadgets
Google
http://techblogon.com/android-fundamentals-components-activity-service-broadcast-receiver-content-provider/

12/13

15/07/2015

Android Fundamentals and Components | Techblogon

Internet
Mobile Phones
Softwares
Technologies
Tutorials
Windows

Pages
About us
Contact us
Privacy Policy

Meta
Log in
Entries RSS
Comments RSS
WordPress.org

2015 Techblogon

http://techblogon.com/android-fundamentals-components-activity-service-broadcast-receiver-content-provider/

13/13

S-ar putea să vă placă și