Sunday, 13 January 2013

Application Components

 Application Components

         Main  Building blocks are components that you use as an application developer to build Android apps.

         Application Components are the essential building blocks of an Android application. Each component is a different point through which the system can enter your application. Not all components are actually entry points for the user and some depend on each other, but each one exists as its own entity and plays a specific role-each one is a unique building block that helps define your application’s overall behavior.
There are four different types of application components.
·      Activities
·      Services
·      Content Providers
·      Broadcast Receivers

                 An activity represents a single screen with a user interface. For example, an message application might have one activity that shows a list of new messages, and another activity to create a new message, and another activity to reading messages.
Although all activities work together to form a cohesive user experience in the messages application, each one independent of the others.

             Services run in the background and don’t have any user interface components. They can perform the same actions as Activities without any user interface. Services are useful for actions . For example , a service might play music in the background while user is in a different application.

Services have a much simpler lifecycle than activities. You start a service , or stop it.
Also, the service lifecycle is more or less controlled by the developer, and not so much by the system. So, we as developers have to be mindful to run our services that they don’t unnecessarily consume shared resources, such as CPU and battery.

Content Providers:
              Content Providers are interfaces for sharing data between applications. Android by default runs each application in its own sandbox so that all data that belongs to an application is totally isolated from other application on the system.

You can store the data in the file system , an SQLite database, on the web, or any other persistent storage location your application can access. Through the Content Provider , other applications can query or even modify the data (if the content provider allows it) .

Broadcast Receivers:
                        A broadcast receiver is a component that responds to system-wide broadcast announcements. Many broadcast originate from the system.

The system itself broadcasts events all the time. For example, when an SMS arrives , or call comes in, or battery runs low, or system gets booted, all those events are broadcasted and any number of receivers could be triggered by them.

You can also send your own broadcasts from one part of your application to another, or a totally different application.

Broadcast receivers themselves do not have any visual representation nor are they actively running in memory . But when triggered, they get to execute some code , such as start an activity, a service , or something else.