Android Application Components
Application components are the essential building blocks of an Android application. These components are loosely coupled by the application manifest file Android Manifest.xml that describes each component of the application and how they interact.
|Activities||They dictate the UI and handle the user interaction to the smart phone screen|
|Services||They handle background processing associated with an application.|
|Broadcast Receivers||They handle communication between Android OS and applications.|
|Content Providers||They handle data and database management issues.|
Apps and APK Files
An Android app is an Android application. An app is packaged in an APK file (Android application package). The APK file contains the compiled Java code and other resources like texts and images for the Android application.
An activity represents a single screen with a user interface. An Android activity is one screen of the Android app’s user interface. In that way an Android activity is very similar to windows in a desktop application. An Android app may contain one or more activities, meaning one or more screens. The Android app starts by showing the main activity, and from there the app may make it possible to open additional activities
Android services are background processes that can be executed on an Android device, even if no application is visible. Services do not need a user interface. A service could for instance check a remote server for updates, or backup data every hour etc. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity.
Broadcast Receivers simply respond to broadcast messages from other applications or from the system. For example, applications can also initiate broadcasts to let other applications know that some data has been downloaded to the device and is available for them to use, so this is broadcast receiver who will intercept this communication and will initiate appropriate action. A broadcast receiver is implemented as a subclass of BroadcastReceiver class and each message is broadcasted as an Intent object.
A content provider component supplies data from one application to others on request. Such requests are handled by the methods of the Content Resolver class. The data may be stored in the file system, the database or somewhere else entirely. A content provider is implemented as a subclass of Content Provider class and must implement a standard set of APIs that enable other applications to perform transactions.
In Android a fragment is a fragment of a total user interface. A fragment typically only takes up part of the screen. Fragments are used inside activities. Fragments can be reused within different activities. Fragments typically contains Views and ViewGroups inside them.
Layout XML Files
Activities, fragments and some ViewGroups can use XML files to define their layout and contents. The layout XML files specify what GUI components an activity or fragment contains, as well as styling of the GUI components (size, margins, padding etc.).
Android intents are small objects that an activity can pass to the Android operating system, to tell the operating system that some other action or activity is required. For instance, a photo app may send an intent to the operating system when the user has chosen to share a photo. The intent describes the “sharing action”. Other applications that knows how to perform the “sharing action” can then be opened, and the sharing can be performed via another application.
Android widgets are GUI components which can be displayed outside of an activity. For instance, a weather widget showing todays weather is shown on many Android home screens. Widgets are implemented and packaged as part of an Android application.
Sometimes Views in Android are also referred to as “widgets”. For instance, many of the GUI components (View subclasses) are located in a Java package called android.widget . But, GUI components are not the same as a widget which can live on the home screen of an Android device. So, be careful when you read about Android to make a distinction between GUI components which can be used inside View Groups, Fragments and Activities (and also inside Widgets), and Widgets which can live on the home screen of the Android device.
Views and View Groups
Android GUI elements fall into three categories: Activities, Views and View Groups. Activities are the screens / windows. Views are the individual GUI elements, like a Text View displaying a text, a Button that users can click on etc. View Groups are containers for Views. A View Group groups a collection of Views together. Views and View Groups can be nested inside an activity or inside a fragment (which is again nested inside an activity).
Resources in Android refer to things like images, strings, and other material that your application uses but is not in the form of some programming language source code. UI layouts are another type of resource. You will create these layouts either using a structured tool, such as an IDE’s drag-and-drop GUI builder, or by hand in XML form. Sometimes, your UI will work across all sorts of devices: phones, tablets, televisions, etc. Sometimes, your UI will need to be tailored for different environments. You will be able to put resources into resource sets that indicate under what circumstances those resources can be used (e.g., use these for normal-sized screens, but use those for larger screens).
The AndroidManifest.xml file contains information of your package, including components of the application such as activities, services, broadcast receivers, content providers etc. It performs some other tasks also:
- It is responsible to protect the application to access any protected parts by providing the permissions.
- It also declares the android api that the application is going to use.
- It lists the instrumentation classes. The instrumentation classes provides profiling and other informations. These informations are removed just before the application is published etc.
This is the required xml file for all the android application and located inside the root directory.
<manifest xmlns:android=”http://schemas.android.com/apk/res/android” package=”com.techPlayOn.hello” android:versionCode=”1″ android:versionName=”1.0″ > <uses-sdk android:minSdkVersion=”8″ android:targetSdkVersion=”15″ /> <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” android:theme=”@style/AppTheme” > <activity android:name=”.MainActivity” android:label=”@string/title_activity_main” > <intent-filter> <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> </application> </manifest>
Elements of the AndroidManifest.xml file
The elements used in the above xml file are described below.
manifest is the root element of the AndroidManifest.xml file. It has package attribute that describes the package name of the activity class.
application is the subelement of the manifest. It includes the namespace declaration. This element contains several subelements that declares the application component such as activity etc. The commonly used attributes are of this element are icon, label, theme etc.
android:icon represents the icon for all the android application components.
android:label works as the default label for all the application components.
android:theme represents a common theme for all the android activities.
activity is the subelement of application and represents an activity that must be defined in the AndroidManifest.xml file. It has many attributes such as label, name, theme, launchMode etc.
android:label represents a label i.e. displayed on the screen.
android:name represents a name for the activity class. It is required attribute.
intent-filter is the sub-element of activity that describes the type of intent to which activity, service or broadcast receiver can respond to.
It adds an action for the intent-filter. The intent-filter must have at least one active element.
It adds a category name to an intent-filter.