Anatomy of an Android Application

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Anatomy of an Android Application

Postby plusminus » Thu Nov 15, 2007 10:54 pm

Source: Google
Google wrote:
Anatomy of an Android Application


There are four building blocks to an Android application:
  • Activity
  • Intent Receiver
  • Service
  • Content Provider
Not every application needs to have all four, but your application will be written with some combination of these.

Once you have decided what components you need for your application, you should list them in a file called AndroidManifest.xml. This is an XML file where you declare the components of your application and what their capabilities and requirements are. See the Android manifest file documentation for complete details.

Activity
Activities are the most common of the four Android building blocks. An activity is usually a single screen in your application. Each activity is implemented as a single class that extends the Activity base class. Your class will display a user interface composed of Views and respond to events. Most applications consist of multiple screens. For example, a text messaging application might have one screen that shows a list of contacts to send messages to, a second screen to write the message to the chosen contact, and other screens to review old messages or change settings. Each of these screens would be implemented as an activity. Moving to another screen is accomplished by a starting a new activity. In some cases an activity may return a value to the previous activity -- for example an activity that lets the user pick a photo would return the chosen photo to the caller.

When a new screen opens, the previous screen is paused and put onto a history stack. The user can navigate backward through previously opened screens in the history. Screens can also choose to be removed from the history stack when it would be inappropriate for them to remain. Android retains history stacks for each application launched from the home screen.

Intent and Intent Filters
Android uses a special class called an Intent to move from screen to screen. An intent describes what an application wants done. The two most important parts of the intent data structure are the action and the data to act upon. Typical values for action are MAIN (the front door of the activity), VIEW, PICK, EDIT, etc. The data is expressed as a URI. For example, to view contact information for a person, you would create an intent with the VIEW action and the data set to a URI representing that person.

There is a related class called an IntentFilter. While an intent is effectively a request to do something, an intent filter is a description of what intents an activity (or intent receiver, see below) is capable of handling. An activity that is able to display contact information for a person would publish an IntentFilter that said that it knows how to handle the action VIEW when applied to data representing a person. Activities publish their IntentFilters in the AndroidManifest.xml file.

Navigating from screen to screen is accomplished by resolving intents. To navigate forward, an activity calls startActivity(myIntent). The system then looks at the intent filters for all installed applications and picks the activity whose intent filters best matches myIntent. The new activity is informed of the intent, which causes it to be launched. The process of resolving intents happens at run time when startActivity is called, which offers two key benefits:

* Activities can reuse functionality from other components simply by making a request in the form of an Intent
* Activities can be replaced at any time by a new Activity with an equivalent IntentFilter


Intent Receiver
You can use an IntentReceiver when you want code in your application to execute in reaction to an external event, for example, when the phone rings, or when the data network is available, or when it's midnight. Intent receivers do not display a UI, although they may use the NotificationManager to alert the user if something interesting has happened. Intent receivers are registered in AndroidManifest.xml, but you can also register them from code using Context.registerReceiver(). Your application does not have to be running for its intent receivers to be called; the system will start your application, if necessary, when an intent receiver is triggered. Applications can also send their own intent broadcasts to others with Context.broadcastIntent().
Service

A Service is code that is long-lived and runs without a UI. A good example of this is a media player playing songs from a play list. In a media player application, there would probably be one or more activities that allow the user to choose songs and start playing them. However, the music playback itself should not be handled by an activity because the user will expect the music to keep playing even after navigating to a new screen. In this case, the media player activity could start a service using Context.startService() to to run in the background to keep the music going. The system will then keep the music playback service running until it has finished. (You can learn more about the priority given to services in the system by reading Lifecycle of an Android Application.) Note that you can connect to a service (and start it if it's not already running) with the Context.bindService() method. When connected to a service, you can communicate with it through an interface exposed by the service. For the music service, this might allow you to pause, rewind, etc.

Content Provider
Applications can store their data in files, an SQLite database, or any other mechanism that makes sense. A content provider, however, is useful if you want your application's data to be shared with other applications. A content provider is a class that implements a standard set of methods to let other applications store and retrieve the type of data that is handled by that content provider.

To get more details on content providers, see Accessing Content Providers.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby tum0rc0re » Mon Dec 03, 2007 11:34 am

I have written new russian article about this theme and a theme about a lifecircle, I have merged their in one article. Russian developers enjoy 8) Here's this article
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

requesting sample code for android app with 4 building block

Postby bob arctor » Mon May 12, 2008 1:24 pm

hi plusminus,

this is a great post :) but i think it could be even greater: by adding a content (or: a pointer to a) basic sample app that deals with fundamental building blocks in android. don't u guys think so too?

bob
bob arctor
Freshman
Freshman
 
Posts: 6
Joined: Fri May 09, 2008 9:10 am
Location: Earth

A simpler tutorial

Postby saigeethamn » Wed Aug 26, 2009 10:56 am

Hi Plusminus,

You have really well worded the Android anatomy.

I have written the same things in a little more simpler way for the real android novice. If any one is interested, you may view the same at :
http://saigeethamn.blogspot.com/2009/08 ... l-for.html
A person who loves to share my ten cents of knowledge
http://saigeethamn.blogspot.com
saigeethamn
Junior Developer
Junior Developer
 
Posts: 15
Joined: Wed Aug 26, 2009 10:51 am

Re: requesting sample code for android app with 4 building b

Postby saigeethamn » Wed Aug 26, 2009 11:55 am

bob arctor wrote:hi plusminus,

this is a great post :) but i think it could be even greater: by adding a content (or: a pointer to a) basic sample app that deals with fundamental building blocks in android. don't u guys think so too?

bob


Bob, you can find a simple example on using "explicit intents" between 2 activities at http://saigeethamn.blogspot.com/2009/08 ... l-for.html

More such examples will soon be available at the same blog.
A person who loves to share my ten cents of knowledge
http://saigeethamn.blogspot.com
saigeethamn
Junior Developer
Junior Developer
 
Posts: 15
Joined: Wed Aug 26, 2009 10:51 am

content provider

Postby nbagade » Sun Nov 22, 2009 6:03 am

Hi Plusminus,

would u like explain Content Provider in more details with example.?
Regards
nbagade
nbagade
Experienced Developer
Experienced Developer
 
Posts: 58
Joined: Fri Nov 20, 2009 7:49 am
Location: India

Top

Postby thetuxracer » Thu Jan 07, 2010 10:23 am

Thank You, it was a bit enlightening. Im just starting on Android Development. Reading your website, very nice.
thetuxracer
Developer
Developer
 
Posts: 26
Joined: Mon Jan 04, 2010 7:59 am
Location: Nashik, MH, India

Postby eyurdakul » Wed Mar 17, 2010 2:53 pm

very nice tutorial plusminus, thank you very much. it is very hard to understand the intent by reading some code examples for a javascript developer with limited java knowledge. this tutorial has enlighted me.
eyurdakul
Junior Developer
Junior Developer
 
Posts: 13
Joined: Thu Mar 04, 2010 1:19 pm

Re: Anatomy of an Android Application

Postby stefan_jacobs » Mon Aug 23, 2010 11:58 am

Thank you plusminus for this informative article. I am a new developer in Android and this has helped me a lot understanding the basics.
Sehnsucht ist so grausam!
stefan_jacobs
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Aug 23, 2010 11:40 am

Re: Anatomy of an Android Application

Postby supriya.dinakaran » Mon Aug 30, 2010 3:44 pm

Hi....
thanks alot for this very useful article.
Could you please tell me if i can get the content provider of another application.
if not how will i get the mime type of another application?
supriya.dinakaran
Junior Developer
Junior Developer
 
Posts: 10
Joined: Tue Aug 10, 2010 11:30 am

Re: Anatomy of an Android Application

Postby erowley » Fri Sep 03, 2010 3:36 pm

I have a question somewhat related to this. I am trying to make sense of all the files and folders that are the source code I downloaded. It included these kinds of folders:

    assets
    bin
    gen
    lib
    libs
    res
    src

And these kinds of files:

    .classpath
    .project
    AndroidManifest -Which you mentioned already, thanks
    default.properties

Where do these get plugged into? Thanks. :D
erowley
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Sep 03, 2010 3:14 pm

Re: Anatomy of an Android Application

Postby Shaista » Wed Oct 20, 2010 9:06 pm

Hi PlusMinus, Thank you so much. very nice tutorial. thanks again :)
Shaista
Freshman
Freshman
 
Posts: 4
Joined: Wed Oct 20, 2010 9:02 pm

Re: Anatomy of an Android Application

Postby melocot » Fri Nov 05, 2010 12:01 am

Thanks PlusMinus, It helps me a lot to understand better how is the structure of an Android application.
melocot
Freshman
Freshman
 
Posts: 4
Joined: Thu Nov 04, 2010 11:55 pm
Location: Spain

Re: Anatomy of an Android Application

Postby superferd » Sun May 15, 2011 7:51 pm

Great post, helps me understand it all a bit more.
-superferd-please help me
Die? over my dead body!
superferd
Junior Developer
Junior Developer
 
Posts: 24
Joined: Sun May 15, 2011 8:23 am

Re: Anatomy of an Android Application

Postby ravi75 » Tue Nov 06, 2012 6:55 pm

Android tutorials for beginners

http://www.chengalva.com

Please check it and post your opinions how it is helpful for you
ravi75
Freshman
Freshman
 
Posts: 6
Joined: Tue Nov 06, 2012 6:51 pm

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 8 guests