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 how they interact.
There are following four main components that can be used within an Android application −
Sr.No
Components & Description
1
They dictate the UI and handle the user interaction to the smart phone screen.
They handle background processing associated with an application.
They handle communication between Android OS and applications.
They handle data and database management issues.
Activities
An activity represents a single screen with a user interface,in-short Activity performs actions on the screen. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. If an application has more than one activity, then one of them should be marked as the activity that is presented when the application is launched.
An activity is implemented as a subclass of Activity class as follows −
Services
A service is a component that runs in the background to perform long-running operations. 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.
A service is implemented as a subclass of Service class as follows −
Broadcast Receivers
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 broadcaster as an Intent object.
Content Providers
A content provider component supplies data from one application to others on request. Such requests are handled by the methods of the ContentResolver 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 ContentProvider class and must implement a standard set of APIs that enable other applications to perform transactions.
We will go through these tags in detail while covering application components in individual chapters.
Additional Components
There are additional components which will be used in the construction of above mentioned entities, their logic, and wiring between them. These components are −
S.No
Components & Description
1
Represents a portion of user interface in an Activity.
UI elements that are drawn on-screen including buttons, lists forms etc.
View hierarchies that control screen format and appearance of the views.
Messages wiring components together.
External elements, such as strings, constants and drawable pictures.
Источник
Components of an Android Application
There are some necessary building blocks that an Android application consists of. These loosely coupled components are bound by the application manifest file which contains the description of each component and how they interact. The manifest file also contains the app’s metadata, its hardware configuration, and platform requirements, external libraries, and required permissions. There are the following main components of an android app:
1. Activities
Activities are said to be the presentation layer of our applications. The UI of our application is built around one or more extensions of the Activity class. By using Fragments and Views, activities set the layout and display the output and also respond to the user’s actions. An activity is implemented as a subclass of class Activity.
Kotlin
To read more, refertothe article:Introduction to Activities in Android
2. Services
Services are like invisible workers of our app. These components run at the backend, updating your data sources and Activities, triggering Notification, and also broadcast Intents. They also perform some tasks when applications are not active. A service can be used as a subclass of class Service:
Kotlin
To read more, refertothe article:Services in Android with Example
3. Content Providers
It is used to manage and persist the application data also typically interacts with the SQL database. They are also responsible for sharing the data beyond the application boundaries. The Content Providers of a particular application can be configured to allow access from other applications, and the Content Providers exposed by other applications can also be configured. A content provider should be a sub-class of the class ContentProvider.
Kotlin
To read more, refertothe article:Content Providers in Android with Example
4. Broadcast Receivers
They are known to be intent listeners as they enable your application to listen to the Intents that satisfy the matching criteria specified by us. Broadcast Receivers make our application react to any received Intent thereby making them perfect for creating event-driven applications.
To read more, refertothe article:Broadcast Receiver in Android With Example
5. Intents
It is a powerful inter-application message-passing framework. They are extensively used throughout Android. Intents can be used to start and stop Activities and Services, to broadcast messages system-wide or to an explicit Activity, Service or Broadcast Receiver or to request action be performed on a particular piece of data.
To read more, refertothe article:Intent and Intent Filters
6. Widgets
These are the small visual application components that you can find on the home screen of the devices. They are a special variation of Broadcast Receivers that allow us to create dynamic, interactive application components for users to embed on their Home Screen.
7. Notifications
Notifications are the application alerts that are used to draw the user’s attention to some particular app event without stealing focus or interrupting the current activity of the user. They are generally used to grab user’s attention when the application is not visible or active, particularly from within a Service or Broadcast Receiver. Examples: E-mail popups, Messenger popups, etc.
To read more, refertothe article:Notifications in Android with Example
Источник
Android SDK: Common Android Components
In this series we are learning the essential features of Android development that you need to know to start building apps. So far, we’ve looked at the structure and typical elements in an Android app, including user interface elements and data storage. You can use what we covered already to start creating your own apps. But before you do, we will run through some common Android components in this tutorial, then have a quick look at the SDK samples in the next tutorial.
Introduction
There are four main Android app components: activities , services , content providers , and broadcast receivers . Whenever you create or use any of them, you must include elements in the project manifest. We’ve met Activities already so I won’t spend any more time on them, but I will go through the other three main app components. I’ll also mention a couple of resources your apps are most likely to use, including fragments and the action bar .
1. Services
A service in Android is a background process. Services are typically used for processes that are ongoing or that take a significant period of time. A service doesn’t have a user interface, so they are often combined with other components such as activities . A typical example is an app in which an activity starts a service running on user interaction, with the service perhaps uploading data to a web resource. The user can continue to interact with the activity while the service runs because it executes in the background.
To add a service component to the manifest, use the following syntax with the element placed inside the application element:
You can create a service class in Eclipse using the same process you would for an activity, choosing service as the superclass. Services are different from the activity components that we’ve looked at previously, in a number of important ways. If you start a service that runs from an activity and the user navigates away from the activity to another app, the service will continue to run. A service therefore has a different lifecycle to the one we explored for activities. You need to bear this in mind to ensure your apps are efficient.
Other app components can bind to services, requesting and receiving data from them. If a bound service is running, it stops when all of the components that are bound to it stop. Although a service is separate from the app user interface, a service started in an activity will run in the same thread as it. However if your service is going to use a significant amount of processing resources, you can create a separate thread to run it in. For more on services, see the Android Developer Guide.
2. Content Providers
A content provider is a component for managing a data set. This data set can be private to your application or can be shared, with other apps able to query and modify the data. If you create a content provider to manage data for your own app, your UI components such as activities will use the content provider, typically through the content resolver class to interact with the data. When used by other apps, the content provider manages access to the data through standard methods to interact with structured data sets such as databases.
If you are at all familiar with relational databases, you intuitively understand the methods used to access data with a content provider. The content provider presents data in a set of tables with rows and columns, and each column within a row (or record) includes a single data value. Handling data returned through a content provider is therefore similar to handling database query results.
Although you may of course create a content provider app at some point, in your initial apps you are far more likely to access one created by another developer or the Android system itself, for example the device calendar or contacts. Content providers can define permissions that client apps are required to have in order to use them. To use a content provider in your app, you need to add the relevant permissions for it in your manifest.
See the Content Providers section in the Android Developer Guide for more information.
3. Broadcast Receivers
The Android system makes various types of broadcasts an app can respond to. You can also develop apps to make these broadcasts, but this is far less likely than to listen for existing broadcasts, at least for your first apps. System announcements include information about the device’s hardware, such as the battery level, the screen shutting off, the charger being plugged into an outlet, etc.
To receive broadcast announcements on Android, your apps can use a broadcast receiver . A typical example of this is a battery level widget in which you want to update the display when the battery level changes. In this case, you could use a service class in conjunction with a broadcast receiver to let your app keep listening for announcements in the background.
The Android system models broadcast announcements as intents , which can be used to start an activity . An intent is an action that the system performs, such as launching an activity or making an announcement. To use a broadcast receiver, your apps must declare it in the manifest, along with an optional intent filter , indicating the actions you want to receive:
This applies to a broadcast receiver in which your app can receive the battery low intent. Note that you can’t receive all system announcements by declaring their actions in the manifest in this way. In some cases you need to register to receive them in Java, for example, with the BATTERY_CHANGED action. See the broadcast receiver class for more on this type of app.
4. Other Classes
As we’ve seen, the Android components are designed to provide interaction between apps. Just as broadcast announcements are available to any app on the system, Android provides certain actions you can use to carry out common tasks in your apps, such as dialing a number. Similarly, you can often use functionality provided by other developers to carry out processing, saving on the amount of code you must implement yourself and letting you focus on the unique aspects of your apps. When you launch one intent from another, you can set it up to return a result to the launching activity, even if the intent that is launched is not part of your app. This lets your app continue to function after the requested action is completed.
Other Android components you should be aware of include fragment and the action bar . Let me briefly run through them:
Fragments
Rather than simply using an activity and a layout to define the whole user interface for each app screen, it is often more efficient to use fragments . With fragments, you can divide the parts of your user interface into logical sections and even reuse those sections in more than one screen of your app. This saves you from the task of implementing the same visual/interactive elements more than once and it gives you a single point of change for these sections. Fragments are modeled as part of an activity, so a fragment is tied to the lifecycle of the activity it is in. See the fragment section of the Developer Guide for more.
Action Bar
The action bar is another key user interface element you may find useful. The action bar gives your app a user interface component that is consistent across the Android system, which makes it an intuitive element for the platform’s users. Typical items displayed in the action bar include an indicator of the user’s location within your app if appropriate and shortcuts to common actions including navigation between the parts of the app. To use the action bar in an activity, make sure that your class extends the ActionBarActivity class and apply a AppCompat theme to the activity in the manifest. See the Developer Guide for more information about the action bar.
Conclusion
Which Android classes and components you use naturally depend on what your app does. However, the above components give you an overview of which classes and components you can choose from. It is often difficult to decide which component or class to use for a particular feature or function, so make sure you understand the purpose of each one before you make a decision. In the next few tutorials, we will look at the Android samples and the app publishing process.