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 developer’s 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.
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: Let’s 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.
2. Intent Filters
Let’s come to the GUI part. The User Interface elements are made up the below components, which we called:
Now it is time to describe in details for each of the above components and its requirement and usages.
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
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 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 it’s work and automatically stops when finish it’s 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.
Don’t worry we will discuss in details about Android Services with an example in the example section.
LifeCycle of a Service in Android
Android Broadcast Receivers
Broadcast Receiver is yet another type of component that can receive and respond to any broadcast announcements.
Let’s 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: sender’s 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. Let’s 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.
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.
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.
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
Intent but 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.
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(“http://www.techblogon.com”));
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
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.
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra(“First Value”, “This First Value for ActivityTwo”);
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.
Let’s 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.