Share your knowledge

Friday 30 December 2016

Application Components





Application components are the essential building blocks of an Android application. These components are loosely coupled by the application manifest file AndroidManifest.xml that describes each component of the application and their interaction.

Four main Components are:

1. Activity
2. Service
3. Broadcast Receiver
4. Content Provider


Let's see simple and short descriptions of main components.



Activity: An activity represents a single screen with a user interface, Lets take an example of contact application, one screen will show all the contacts(Activity -1), clicking on  New contact will show another screen to add contact(Activity -2).

Activity -1























Activity -2 























Note: Activity serve as the entry point for a user's interaction with an app.

An Activity is implemented as a subclass of Activity class as below...

public class MainActivity extends Activity {
System.out.println("Building Activity");
}

Service: A service runs in the background to perform long-running operations. which doesn't contain any user interface. For example, a service might play music in the background while the user is in a different application.

other examples include: fetching wifi status, network status in the background.

A service is implemented as a subclass of Service class as below...


public class MyService extends Service {
System.out.println("Building Service");
}



Broadcast Receivers: These are registered for system announcements.
Ex: 1. When you plug-in a headset, headset icon will come in the status bar as below.























Mobile without headset connected
























Mobile with headset connected

2. when you plug-in a charger, charger symbol will come
3. when you click on volume button to increase/decrease volume, how volume will get change?
























Everything is done by Broadcast Receivers.

Note: It provides communication between android os and applications.

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

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



Content Providers: A content provider supplies data from one application to other application on request. Such requests are handled by the methods of the ContentResolver class. It's not possible to share data from one application to another application without Content Resolvers.

use-case:  Getting all the contacts information from contacts application to What's app application.

A content provider is implemented as a subclass of ContentProvider class and must implement a standard set of APIs that enable other applications to perform transactions.


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




We will check these components and other components in detail in their individual chapters.

 Let's build your first application



No comments:

Post a Comment