In this blog, we are going to learn about the “Android Components.” So before we proceed further, let’s talk about why there is a need to learn these components. Can we learn about Android without having the knowledge about it?
Let’s examine “components” in “Android Components”. If we go through the basic definition of components, it states that “Component is a part that combines with other parts to form something bigger.” So in other words, we can say that they are essential elements to form basically anything. Like in the case of a computer, we need a motherboard, CPU, keyboard, etc., these become the basic components needed to build a computer. And it is important to know about all these different components that help build a PC.
Similarly, we should know about Android Components, which are often also known as the basic building blocks, because without them it is not possible to develop Android applications.
Four basic components of Android:
- Content Provider
- Broadcast Receiver
Let us have a look at these components one by one.
If you look at the meaning of a Normal Activity, it is “the condition under which things are happening.”
Let’s make this definition even clearer. If you remember your school days, where there would be a “Lab Activity,” where each of you would perform or draw something according to the tasks assigned to you by your teacher that came with conditions.
This is how “Activities in Android” also behave.
So what is an Activity in Android?
Activity is the entry point in an Android application that a user can interact with. To be more clear, an Activity is each and every single screen that you look at your Android device to interact with.
Go ahead and take a look at your phone, if you touch anywhere on the screen, you get to see the lock screen, which is in effect an “activity.” This is the screen where the user (you) gets to interact, i.e, unlock the screen (which is then completely based on your lock pattern), the post which, you are led to the “Home Screen.”
This was just an example of a simple activity, there are many applications on Android, where, a user has a range of activities to engage in.
If you click on any application, it opens another page which is also an activity.
Now, what is the use of an activity in our devices?
Got an answer? That’s okay. We’re here to help you with it.
Activities are used to make interactions with the system (device) and the apps.
Now you may ask? What kind of interactions?
Here are some interactions which an activity usually makes between a system and an app:
- It keeps a track of what a user is currently using so that the system keeps hosting the activity.
- It will also keep a track of your previously used applications and arrange it in a stack according to priority (which is based on the time elapsed since the app was last used).
- You can see that stack by pressing the recently used app button.
- It will also keep a record of previously paused activities so that a user can return to it. For example, if you are playing a game and suddenly your phone rings, then the game app will go into a paused state so that you can later return to the activity as you left it.
- It also provides a way for applications so that a user can switch between it and the system that allows that flow.
For more information on Activities, refer the following link:
The word “Service” has different meanings in different terms. You can relate it to the vehicle service or it can be a customer service, or whatever you can think.
In Android, “Services” is a component that runs in the background so that it can perform long, running operations or perform work for remote processes.
Understanding With the Help Of an Example:-
Let’s understand this better. It is saying that “services” is that component in Android which is used to run certain tasks in the background. The users do not need to worry about them, it is the system’s responsibility to run them.
Suppose you have an automatic call recorder app on your device and when a call comes, then it automatically starts recording the call.
So how did it start recording, even though the app was not open?
This is because when the call is incoming, then it is also starting an activity, and this activity is calling the bindService() method and allowing the automatic call recorder app to bind with it.
This app will bind to the call activity as long as the call is running and get destroyed when the call is disconnected.
This is the example of the Bound Type Service.
Now let us take another example: Suppose while listening to music on your phone, you’re are also using the Facebook app or your device is downloading some files without stopping an activity or updating your other apps. So do you ever think, how this must be happening?
This is just because of this Bound Type Service component. It basically lets the music app play in the background and lets the device download files or update apps in the background without stopping the current music playing activity.
This is an example of the Started Type Service. In this, a service is started when an activity calls the method startService(). And in this, the background app runs until its task is finished irrespective of whether the current activity is running or stopped. You have seen many times that the music will not stop if you logout from Facebook or even when your phone gets locked, the music still continues to play.
There is another type of a Service in Android that is “Scheduled.” This is a very special type of service that can schedule any task in your device at a respective time when you want that task to execute. It uses a special API jobScheduler that is introduced in API Level 21(Android 5.0).
For example, nowadays, in Android devices, when a device is asking for system updates, there is an option known as Installed Overnight, in which you can select a particular time at which you want to install the update because it needs a high internet speed and also restart several times.
In theory, this looks ok, but how can we implement this in an Android or in a real application (like coding). I’ve put a link below which you can refer to.
Services in deep.
Before discussing this component, we have a question for you.
In your device (phone), which is the most important system app for you?
You may think, what a silly question, but it’s very obvious that the phone and the gallery app contain your contact and images respectively, this is most precious data that I have.
Here, I want to point out to one more thing, that data cannot be shared between two applications. But wait, now you may think how is it that Whatsapp is able to use my contacts? Confused? Let’s discuss in detail.
Data is very similar to content. So while downloading any application, you might have noticed that before downloading, it is asking for some permissions.
So by accepting the terms and conditions, this app uses data from these listed apps. This can be done by the use of the content provider.
Content Provider: The meaning of this component resides in the word itself as it is used to provide the content(data) or to access the data by other applications which is stored by itself.
The most important use of content provider is that you can configure a content provider to allow other applications to securely access and modify your app data as listed in the figure.
Figure: Overview diagram of Content Provider.
So according to this figure, if your application is implementing the Content Provider, then it may allow other applications to use the data, otherwise, it does not.
Now suppose that you don’t want to share your application data, even then you’ll be able to use this as it will provide you an excellent level of abstraction and data security.
This will allow you to make modifications in our app without affecting the other app that is using your data.
This can be more clear by the help of the following figure:
Figure: Migrating Content Provider Storage
So according to this figure, even through the use of Content Provider, you have modified your app database so that other applications can use data which you want them to use.
Advantage of Content Provider
Content providers offer different controls over the permission to access your app data.
Like you can choose to restrict access, or you can grant blanket access, or you can configure different reading or writing permissions.
For more information, see Security tips for storing data.
To get a much better understanding, see the Content provider in Full Detail.
Before going further, let’s have a look at the term called BroadCast.
Just go back to your school days, suppose you are sitting in your class and suddenly, the Math Teacher starts calling out your name because did not do your homework. In this scenario, the whole class is going to listen to your name, but who will respond? Only you.
Now you may think, how is it related to Broadcasting?
Yes, it is related, when a teacher is calling out your name, it is similar to broadcasting the message so that the appropriate person will respond towards it.
In our day-to-day life, we hear many broadcasts, but we ignore it because it is of no use for us. Like how only those who need vegetables will respond to the seller broadcasting his price rates for veggies.
Now you will ask, how is this thing related to Android? Does our device also act like the vegetable seller?
“Broadcast Receiver” is self-explanatory. It is used to receive Broadcast Messages from the
System. Let’s make it more clear, when you plug in the charger, then the charging sign will automatically get displayed on the battery icon.
In whichever activity you are, when the phone is charging, then the sign will automatically come up. Let’s have another scenario, when you set alarms to give you notifications, the system will tell inform it to you in the form of a notification.
Much of the broadcasting is done by the system, like when your battery is low or when the download is done or when the screen is locked, etc.
So how are these things happening?
All the above types of things can be done through the broadcast receivers. It will allow the System to deliver broadcasts even when applications are not currently running. Although broadcast receivers don’t display a user interface, but they will show a status bar notification at the top.
To know how to implement this, refer to Broadcast Receivers In Deep.
Hope you got to know about the basic building blocks of Android.
Keep Reading! Keep Learning! Stay Tuned!