Android Components : Four pillars of every Android app

Spread knowledge

Before beginning with the actual app development it is very important for every developer to be aware of the four Android Components. These are building blocks of an Application. Any feature that we see in an app is the result of interaction between these components. A developer needs to declare all the Android components he is using inside the AndroidManifest.xml, this what informs the system of number and names of components used in this app.

The four components are

  • Activity
  • Service
  • Content providers
  • Broadcast Receivers

Activity

This is most popular of all the Android Components. Mostly because this is the only UI  component. An activity represents a single screen user interface and any action you perform on the screen is the part of the activity. In simple terms, an activity is to android what a webpage is to a website (though they don’t have any technical connection). When ever you go to website you see a page when you click on anything on this page you are taken to an another page. Similarly when ever you open an Android app you see something like  a “page”. This page over here is an activity. When ever you click anywhere on this activity you are taken  an another activity.

Any activity which you are using in the app needs to be declared inside the  AndroidManifest.xml. Of all the activities declared one needs to be specified as the “Launcher” activity. This is the activity which you see the when you launch the app.

To design your own activity you need to just extend Activity class

public class MainActivity extends Activity {



}

There is a lot more to activities than explained above. This is just an introduction. You can read more about activities here

Photo_1501002907796-1

Service

I hope by now you have understood what exactly is an activity . But think of a task which needs t be performed without a UI. Consider you want a background music to be played while you the user navigates the app. If you add the music in an activity it will automatically stop when the user navigates to another activity. This is where services come into the picture. Services is a component without a UI. It can be used to perform a long running operations which don’t require a UI. Once started these services keeps on running until you explicitly stop it. Services are always started by some another component. You cannot just have a launcher service like you had a launcher activity. If you wish to start a service when app start you need to start it from the launcher activity.

To implement a service you just need to extend the Service class

public class MyService extends Service {


}

 

This is just an introduction. You can read more about services here

Photo_1501002592183

Content Providers

Now imagine your app needs access to the device contacts or the gallery photos. How do you think the Gallery or Contacts app will provide your app with these information. Even you would sometimes like to share some information from your app’s database  to other apps. This whole mechanism of sharing information between apps works on the concept of Content Resolver and Content Provider. To share any information with other apps you will have to provide a content provider and implement some sets of API. Similarly, requests for information from other apps are handled by Content Resolver.

public class MyContentProvider extends  ContentProvider {
  
   public void onCreate(){

   }

}

Broadcast Receiver

Broadcast Receiver simply listen for broadcast messages from other Applications and the System . Suppose you want your app to be notified whenever the battery is down or any other System Event is fired, you can add a Broadcast Receiver in your app which listens for the specified event. Whenever the system or that “other Application” fires the specified event the code in your Broadcast Receiver starts executing. You can even program your  app to send Broadcast messages and receive it in the app itself. Hence if used properly Broadcast receiver could be very helpful in enabling lot of features

A broadcast receiver is implemented as a subclass of BroadcastReceiver and each message is broadcaster as an Intent object.

public class MyReceiver  extends  BroadcastReceiver {
   
   public void onReceive(context,intent){
   }

}

You can read more about BroastcastReceiver here.

Lets Conclude

Well, this is not it. Though we have understood all the major Android Components, they are a lot of other “small” components which play a major role in app development. But this is a good start!


Spread knowledge

4 Replies to “Android Components : Four pillars of every Android app”

Leave a Reply

Your email address will not be published. Required fields are marked *