By Md. Sabuj Sarker | 11/13/2017 | General |Beginners

Components of an Android Application

Components of an Android Application

Since its beginning, Android provided a very modular system for programmers to work with. At its core Android is a very modular system. It also provides good abstraction over many aspects to the development process. As a modular approach Android applications are mainly divided into four main components. They are:

  • Activities
  • Services
  • Broadcast Receivers
  • Content Providers

I am going to provide you a good overview of these four components in a way that will help you to grasp their core concepts faster.

Activities

The developers of the Android operating system were very aesthetic in naming the components. At first glance of the name activity, it may seem to a non-android programmer that it is some kind of thread, process, method or related thing—but it is not. An activity represents a single screen. In desktop GUI programming we have a similar concept that we call window. In some GUI frameworks there are concepts called screen. But, ultimately there is no direct one-to-one 100% comparison between desktop and Android systems. Open any application in your Android phone or tablet and what you see on the display is called an activity. It is the visible part of the application. You have to interact with the application with some graphical components through activities.

An application can have more than one activity and activities have different life cycle phases. If you are in a web browser, browsing some web site when suddenly your phone rings, you see a different activity to receive or reject the call. At this time the web browser's activity is gone from the screen, though the web browser's activity is still consuming some memory and other resources. It is not definite how long you will stay on the phone activity. Wasting is never a good thing and thus the Android system will clean up the memory and resources the web browser activity is taking up. So, Android has a good system architecture design. To kill, pause, resume an activity, Android does not do this mercilessly. It wants to notify the application before hand, giving the application a chance to do some housekeeping and homework.

Services

Service is one of those essential components that almost no Android application can live without. It is also the component that confuses the beginner Android programmers most. Services live throughout the lifetime of the app unless explicitly killed. The big misconception that many beginning Android programmer have is mistakenly thinking that it is a conventional operating system service.

Unlike activities, services are run in the background. By background we mean not through user interface, though this does not necessarily mean that it is running in another thread. So, services can perform long-running operations in the background and it does not provide an user interface. If all the activities of an application are paused or killed, services can still run.

A common misconception about services is that they do not execute in the main thread. But that's not true. Services do run in the main thread. The UI also lives in the main thread. That means if you run some code directly inside a service that takes long time, and may result in an unresponsive application. Instead, services should execute long running tasks in a separate thread.

Like activities, services have life cycles with different phases. You can tap into them for performing different housekeeping and homework things.

Broadcast Receivers

Different kinds of things always happen inside an operating system. With a lot of external communication devices attached, a lot more things happen inside an Android operating system. It's a very noisy place inside Android. A lot of events takes place all the time. Whenever an SMS is received an event is generated, when a call comes in another event makes more noise, different sensors also creates events. The events are broadcasted from the Android system or different installed applications. Applications can catch those broadcasts to fulfill some agenda. To tap into those system wide broadcasted events you need to tap into them with the help of broadcast receivers. Your application can also publish custom broadcasts or events to the system for other application to consume them.

Content Providers

Passing data from one application to another and keeping it secure or private is a very tough task in different operating systems. Two different applications run in two different separate parts of a system and every process has a separate virtual memory area. So, we cannot access directly one application’s data from the another. We need some kind of IPC (Inter Process Communication) to exchange data between applications. Again, it is not a wise idea to let one application access private files directly from another application. Instead, it is better to pass the data without letting the other application know how the application stores its data. Abstraction is necessary here.

To exchange data between different applications in an Android system we do not need to worry about managing the Inter Process Communication, about private data safety, or any other difficult things. Android manages them all by itself for us if we abide by its API for doing so. The fourth component in the list is called Content Provider and helps us in doing so. Content providers provide methods for CRUD operations among applications. But do not be misguided by CRUD that the data must be stored in some kind of database. You can store data in any form you like in your application. You can provide the data with the help of the CRUD methods from the provider to the client applications. The clients or consumer applications cannot call the CRUD methods of the provider applications directly. It will have to use the Content Resolver's equivalent methods for interacting with the provider.

Conclusions

Being modular and solving problems with different pattern implementations,Android has made the life of programmers very easy. Aside from that, it helps us scale our applications easily when things get really complex and messy by providing standard APIs. Also, Android provides compatibility layers and libraries when it updates some part of it's system. To get along well with the system you need be well acquainted with its different parts, though it may take time to grasp the concepts. But it is advised to take the time to understand them without blindly coding just to get the work done.

 

Looking for some more hands-on Android? Check out: Working with Content Providers in Android.

Or stop by the homepage for tons of Andorid Development Tools.

By Md. Sabuj Sarker | 11/13/2017 | General

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Recent Stories

Top DiscoverSDK Experts

User photo
3355
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
3220
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
3060
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All
X

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.

{{compareToolModel.Error}}

Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now