4 Core Android Building Blocks and Component | Application Examples
As all of us are aware, with growing times, Android has gained a lot of booms.
With the evolution of the smartphone, the need for better and handy mobile applications has grown with time. And this need has brought in competition amongst the companies to keep them tangible in the market by making their services accessible to end-user without much hustle.
And ‘Android’ is the major contributor that’s calming this chaos.
I guess Android doesn’t need its introduction… Does it?
Android is an operating system with its foundation on an adapted version of Linux kernel, developed by Google. It is open-source and is majorly designed for touch screen devices like smartphones.
In an earlier tutorial, we have seen Android Navigation Architecture components. Now let us proceed towards the gist of this tutorial.
What are the Android building blocks? Does Android have any? Can we design an Android application without the help of these building blocks?
Too many questions may be clouding over, so let us get to know about them one by one.
What is Android Building Blocks?
Building blocks, the name itself suggests that these are the essentials to design something bigger and better.
To be specific, to create a concrete structure we need bricks, cement, sand, mortar, etc. and that is how we build a house or any concrete building.
Similarly, to design any android application, we need to know about these core building blocks or simply let’s call them ‘Android Components’.
What are Different Types of Android Building Blocks?
There are 4 core components of Android architectures, namely –
- Broadcast Receiver
- Content Provider
Let us now take a quick ride to each one of them to get to know them better. The first stop is Activity.
In general, an activity refers to a situation due to which plenty of other things are happening.
In Android, an activity is the point of the source through which the user can interact in an Android application.
An activity basically is a single screen with a user interface. For instance, if you open any messaging application, you may have one activity to read the messages, one activity to compose new messages, and so on.
While all the activities perform their tasks together as a single entity of messaging application for the user, each of the activities is internally independent of the erstwhile.
Likewise, if the user wants to share the contact details of any other person through the messaging application, then such action can be performed, i.e., a different app can access and start its activities if the currently running application allows it.
With the help of activities, system can interact with the applications.
Below are few of the notable interactions amid system and application:
- Keeps a record of what a user is currently using such that the processes which engage the host activity keep running.
- Keeping a trace of previously run processes/applications and stack it as per priority knowing that users may run them later.
- Keeps a trace of formerly paused applications, in case the user wishes to return back to it. For instance, if the user is watching a video and all of a sudden receives a call, then the video moves to a paused state until the user returns back to it.
- It provides way to co-ordinate amongst different applications and allows the system to maintain flow amidst them.
How to implement Activity block?
To implement an Activity in the code base, one needs to create a sub class of Activity class.
As we are aware, a program starts with the
main() method in Java. Same way, Android starts its process through the
onCreate() callback method within the Activity class.
We will learn the lifecycle of an Activity in upcoming tutorials.
Interesting! Our next halt is service..
In Android, services are components that allow us to run an application in the backdrop for any probable motive.
Let us consider that the user is playing some good music and a notification pops up, the user clicks and opens the notification to get to know what it, thereby pushing the music application to the background.
How to create Service block in Android?
To implement a Service in the code base, one needs to create sub class of Service class.
Android defines three diverse kinds of services, namely –
It is visible to the user and keeps running until the user stops it. For such services, a notification is always displayed in the notification panel. For example, the user is listening to some audiobooks and also surfing the net at the same time. Such services continue to run without user intervention.
It is barely visible to the user but performs its tasks. For instance, if the system is performing a few tasks, then it would usually be a background service.
A service is considered to be bound to some specific application if an application calls the bindService(). Abound service runs till the time an application component is bound to it.
We will learn more about Services in Android in upcoming tutorials.
The third stop of our ride to android components in the broadcast receiver. Come, let’s explore it.
3. BROADCAST RECEIVER
In our day to day life, we come across lots of broadcasts.
For instance, if the latest trending cum affordable courses advertisement banner flashes on the screen in some website, then those interested in applying, exploring, and learning something new respond to it by clicking on it and enrolling in it, while others tend to ignore it.
Similarly, a broadcast receiver in Android basically receives a broadcast message from the system.
Example and Application of Broadcast Receiver:
Simplifying it, when you plug-in charger to your smartphone, then no matter whatever activity you are actively working on, the charging sign appears comes into sight on the screen automatically.
You have set some reminder alarm to perform some task at a specific time, then the system will inform about it with the help of notification and some alarm tone.
The majority of the broadcasts are received from the system itself, like, low battery, charger connected, screenshot captured, etc.
Thus, broadcast receivers are those components of Android that allow the system to convey messages to the user outside the current flow even, by making it a system-wide announcement.
How to implement Broadcast Receiver in Android?
To implement a broadcast receiver in the code base, one needs to create a subclass of the BroadcastReceiver class.
Oh yes !! We are almost there. The last destination is content provider…
4. CONTENT PROVIDER
The name is itself self-explanatory, content provider – one that provides access to content.
You might be wondering how we can share data between two different applications.
So here it is.
What is purpose of Content Provider Android building blocks?
If you recollect whenever we download any application, it requests permissions, and by accepting those requests we provide permissions to access and use data from different applications.
And, all this is possible with the sheer help of content provider in Android.
How to implement Content Provider Block in Android?
A content provider handles data with the help of a file system, SQLite database, JSON, or any other stable form of storage.
To make use of the content provider, you must carefully configure it to your application to allow other apps to securely access your data and modify it as per need.
With content providers, we can restrict access, provide layered/blanket access or play with the read, and write permissions of important data.
Application of Content Provider Block:
For better understanding, consider the Whatsapp application.
We provide access to our contacts to it and to update status we grant permission to open camera/gallery. All of this is done by creating a sub- class of the ContentProvider base class.
In such cases, the system starts any other app’s component that is not a part of the current app. i.e., WhatsApp starts the activity to capture a picture in the camera app, it runs the processes that are related to camera app (mainly those that are not a part of the WhatsApp).
Henceforth, like Java, the main() function is not the only entry point in the Android app (Note: there is no method called main() in Android).
Thats not it, there is a surprise element – the manifest file in Android.
Manifest File in Android
To start any android component, the component must exist in the
AndroidManifest.xml file to be read and processed.
The manifest file consists of
- app permissions such as internet access, camera access, read/write access to the contact list, etc.
- minimum API level declarations required to run the app,
- declarations of hardware or software features, such as Bluetooth, camera, etc.
- declarations of any other API libraries that are being used in the design and implementation of the application.
To declare our building blocks of android in the manifest file we use the below tags.
1. activity elements
2. service elements
3. broadcast receiver elements
4. content provider elements
And that’s the end of the ride to the core Android building blocks and components of Android architecture.
To sum it up, we learned about activities, services, broadcast receivers, and content providers in brief. You can use these blocks to create an Android calculator app.
Apart from that, we even got to know the basics of the Android manifest file.