July 1, 2013

Fundamental Concepts of Android Development

In this article, I'm share with you all the basic concepts related to android development which should be know before you start development. Let's start now!

* Fundamental Building Blocks

  • Activity
  • Service
  • Broadcast Receiver
  • Content Provider

* Communication Between Component Through

  • Intents
  • Intent Filters

* User Interface Elements

  • Views
  • Notification

1) Activity:

Basic building block of every visible android applications. It render a UI. Every scree in application is itself an activity. They work together to present application sequence while each activity is an independent activity.

2) Service:

Basic building android applications which doesn't provide a UI. It is a program that can run in background for an indefinite period.

3) Broadcast Receiver:

Another component that can receive and respond to any broadcast announcements.

4) Content Provider:

Separate league of component that expose a specific set of data to application.

5) Intents:

These are messages that passes between components. It is equivalent to parameters passed to API calls? Yes it is close to that.

However, the fundamental differences between API calls and intents way of invoking component is;
  • API calls are synchronous while intent-based invocation is asynchronous (mostly).
  • API calls are bound at compile time while intent-based calls/invocation are run time bound (mostly)
Intent is a bundle of information, a positive data structure that holds an abstract description of operations to be performed. Communication happens through intents between two activities are;
  • Explicit Intents
  • Implicit Intents

i) Explicit Intents:

Specify all activity that is required to respond to the intents. You explicitly designate the target component. This is typically used for application internal messages.

ii) Implicit Intents:

The main power of android design, you just declare an intent and leave it to the platform to find an activity that can respond to intents. Here, you don't declare a target component and hence in typically used for activating components seamlessly.

A) Explicit Intent Example:

  • Invoking Activity
  • Invoked Activity

a) InvokingActivtiy:

It has a button "Invoke Next Activity" which when clicked explicitly calls the "InvokedActivity" class.


Button btnInvoking = 
    (Button)findViewById(R.Id.invokedButton);
btnInvoking.setOnClickListner(new onClickListner()
{
    public void onClick(View v)
    {
        Intent iExplicit = 
            new Intent(InvokingActivity.this, InvokedActivity.Class);
        startActivity(iExplicit);
    }
});

Note: The layout for InvokingActivity is defined in main.xml and for InvokedActivity in InvokedActivity.xml

B) Implicit Intent Example:

An implicit intent doesn't name the target component that should act upon the intents. An intent object has the following information which is of interest for implicit intents; These are intent filters
  • Action
  • Category
  • Data
Intent filters that are declared target components who are willing to accept implicit intent calls. This is done declaratively in AndroidManifest.xml file.

* Important Points:

  • Implicit intents don't specify a target components.
  • Components willing to receive implicit intents have to declare their ability to handle a specify intents by declaring intent filters.
  • A component can declare any number of intent filters.
  • You can set priorities for intent filters to ensure the order of responses.
The InvokeImplicitIntent activity creates an intent of implicit objects "contact". This intent objects component is not set. However, the action is set to "android.content.intent.ACTION_VIEW" and the data's URL is set to "People.CONTENT.URI".

Intent matches intent filters declared by view contacts native activity. So when you run this application, it displays the native UI for viewing the existing contacts on the phone!

Button viewContacts = 
    (Button)findViewById(R.Id.ViewContacts);
viewContacts.setOnClickListner(new onClickListner()
{
    public void onClick(View v)
    {
        Intent contacts = new Intent();
        contacts.setAction(android.content.Intent.ACTION_VIEW);
        contacts.setData(People.CONTENT_URI);
        startActivity(contacts);
    }
});

In this manner, many of native applications can be seamlessly invoked as one of the activities in our applications through implicit intents.

6) Intent Filters:

Through which component advertises its own capabilities to handle specify job/operation to the android platform.