MA Unit2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

Anatomy of Android Application

An Android application consists of several components that


work together to provide the functionality and user
experience. Here's a breakdown of these components:
1. Activities: Activities are the building blocks of an Android
application's user interface (UI). Each activity represents a
single screen with a user interface. Activities are responsible
for handling user interactions, such as button clicks, touch
events, etc. An application can have multiple activities that
interact with each other through intents.
2. Fragments: Fragments represent a portion of the user
interface in an activity. They are reusable UI components that
can be combined within a single activity to build a multi-pane
UI for tablets or a dynamic UI for smartphones. Fragments
have their lifecycle and can be added, removed, or replaced
within an activity dynamically.
3. Services: Services are components that run in the
background to perform long-running operations or to handle
network transactions. They don't have a user interface, and
they run independently of activities. Examples of services
include playing music in the background, downloading files,
or fetching data from a server.
4. Content Providers: Content providers manage access to a
structured set of data. They encapsulate the data and provide
mechanisms for defining data security. Content providers are
commonly used to share data between applications or to
provide data to widgets, such as the Contacts or Calendar
content provider.
5. Manifest File: The AndroidManifest.xml file describes
essential information about the application to the Android
system. It contains information such as the application's
package name, components (activities, services, broadcast
receivers), permissions required by the application, hardware
and software features used by the application, etc.
6. Resources: Resources are external elements such as
images, strings, layouts, and styles that are separate from the
application code. They are stored in the res/ directory of the
application and are referenced in the code using resource IDs
generated by the Android Asset Packaging Tool (AAPT).
7. Layouts: Layouts define the structure of the user interface.
They are XML files that specify the arrangement of UI
components such as buttons, text fields, images, etc., within
an activity or fragment. Android provides several layout
types, including LinearLayout, RelativeLayout,
ConstraintLayout, etc., to organize UI components efficiently.
8. Intents: Intents are messaging objects that allow
components to request functionality from other components or
to announce events. They are used to start activities, services,
or broadcast messages to other components. Intents can be
explicit (targeting a specific component) or implicit (allowing
the system to choose the appropriate component).

Android Terminologies
1. Android: Android is a mobile operating system
developed by Google. It is based on a modified version of the
Linux kernel and other open-source software and is designed
primarily for touchscreen mobile devices such as smartphones
and tablets.
2. APK (Android Package): An APK is the file format
used to distribute and install applications on Android devices.
It contains all the elements that an app needs to install
correctly on an Android device, including code, resources,
assets, and manifest file.
3. SDK (Software Development Kit): The Android SDK
is a set of development tools that enable developers to create
applications for the Android platform. It includes a
comprehensive set of libraries, debugging tools, emulators,
and documentation.
4. API (Application Programming Interface): In the
context of Android development, an API is a set of rules and
protocols that allow different software applications to
communicate with each other. Android provides various APIs
for developers to access system features such as camera,
sensors, location, and more.
5. UI (User Interface): UI refers to the graphical layout of
an application, including elements such as buttons, text fields,
images, and navigation menus. Android provides various UI
components and layouts for developers to design their app
interfaces.
6. Activity: An Activity is a single, focused task that the
user can interact with in an Android application. It represents
a single screen with a user interface. Activities are the
building blocks of an Android app and are typically defined in
the app's manifest file.
7. Fragment: A Fragment is a modular section of an
activity that has its own lifecycle, layout, and behavior.
Fragments are often used to create more flexible and reusable
UI components within an activity.
8. Intent: An Intent is a messaging object that is used to
request an action from another app component, such as
starting a new activity, broadcasting a message, or delivering
a data payload. Intents are a fundamental part of Android's
inter-application communication system.
9. Layout: A Layout is a structured arrangement of UI
elements within an Android application. It defines the visual
structure and appearance of the user interface, including the
placement and sizing of buttons, text fields, images, and other
components.
10. Manifest: The AndroidManifest.xml file is a
configuration file that contains essential information about an
Android application, such as its package name, version,
permissions, activities, services, and broadcast receivers. It is
required for every Android app and is used by the Android
system to identify and manage the app's components.
11. Gradle: Gradle is the build automation tool used for
building, testing, and deploying Android applications. It
allows developers to define the build configuration,
dependencies, and tasks for their projects using a Groovy-
based DSL (Domain-Specific Language).
12. ADB (Android Debug Bridge): ADB is a command-
line tool that allows developers to communicate with an
Android device or emulator from a computer. It can be used to
install and debug applications, transfer files, and access
various device functionalities for testing and development
purposes.

Application Context
What is Context?
In Android, Context is an abstract class whose
implementation is provided by the Android system. It
represents various aspects of an application's environment,
such as its current state, resources, and access to application-
specific operations.
Types of Context:
In Android, there are two primary types of Context:
 Application Context: Represents the overall
environment of the application. It's tied to the lifecycle of
the application and is not tied to any specific activity. It's
accessible throughout the entire lifespan of the
application.
 Activity Context: Represents the environment of a
specific activity within the application. It's tied to the
lifecycle of that activity and provides access to activity-
specific resources and operations.

Access to Resources: One of the key functions of the


Application Context is to provide access to application-
specific resources, such as strings, colors, drawables, and
layouts. These resources are typically stored in the res
directory of the application and are accessible through
methods provided by the Context class.
Access to System Services: Context allows access to various
system services provided by the Android platform, such as
accessing the database using
Context.getSystemService(Context.DB_SERVICE),
obtaining the connectivity service, accessing the location
service, etc.

Access to Application-specific Classes: Application Context


also enables access to application-specific classes, including
activities, services, broadcast receivers, and content providers.
It can be used to start activities, bind to services, and access
content providers.

Contextual Information: Besides providing access to


resources and services, Context also contains contextual
information about the application environment, such as the
package name, file directories, application metadata, etc.
Overall, the Application Context in Android is a crucial
component that provides essential information about an
application's environment and facilitates access to application-
specific resources, services, and operations throughout the
lifespan of the application. Understanding how to use and
manage the Application Context effectively is essential for
building robust and efficient Android applications.

Activities of Android Application


The Activity instances in an Android app go through various
states in their lifecycle as the user navigates through, exits,
and re-enters the app. The Activity class offers several
callback methods that inform the activity of state changes,
such as creation, stopping, resuming, and destruction of the
activity process.
To navigate transitions between stages of the activity
lifecycle, the Activity class provides a core set of six
callbacks: onCreate(), onStart(), onResume(), onPause(),
onStop(), and onDestroy(). The system invokes each of these
callbacks as an activity enters a new state.
The above image is the easiest illustration of the activity
lifecycle.
onCreate()
This fires when the system first creates the activity. On
activity creation, the activity enters the Created state. In the
onCreate() method, you perform basic application startup
logic that should happen only once for the entire life of the
activity. For example, binding layout or your ViewModel.
onStart()
When the activity enters the Started state, the system invokes
this callback. The onStart() call makes the activity visible to
the user, as the app prepares for the activity to enter the
foreground and become interactive. Use this where the app
initializes the code that maintains the UI, like starting
animation or attaching listeners or receivers.
onResume()
When the activity enters the Resumed state, it comes to the
foreground, and then the system invokes the onResume()
callback. This is the state in which the app interacts with the
user. The app stays in this state until something happens to
take focus away from the app. Such an event might be, for
instance, receiving a phone call, the user navigating to another
activity, or the device screen turning off.
onPause()
The onPause() callback method is called when an activity is
no longer in the foreground, either because another activity
has taken focus or because the user has navigated away from
the app. In this method, you should perform any necessary
actions that should happen before the activity becomes
partially or completely obscured, like stopping animation or
persisting UI states.
onStop()
This method is called when an activity is no longer visible to
the user and is in the process of being stopped. In this method,
you should perform any necessary actions that should be done
when the activity is no longer visible. Like, releasing the
resources, and unregistering listeners or receivers.
onPause() is called when an activity is losing focus, while
onStop() is called when the activity is no longer visible to the
user.
onDestroy()
onDestroy() is called before the activity is destroyed. The
system invokes this callback either because:
1. the activity is finishing (due to the user completely
dismissing the activity or due to finish() being called on the
activity), or
2. the system is temporarily destroying the activity due to a
configuration change (such as device rotation or multi-
window mode).
Instead of putting logic in your Activity to determine why it is
being destroyed, you should use a ViewModel object to
contain the relevant view data for your Activity.

Services of Android Application


Services in Android are a special component that facilitates
an application to run in the background in order to perform
long-running operation tasks. The prime aim of a service is to
ensure that the application remains active in the background
so that the user can operate multiple applications at the same
time. A user-interface is not desirable for android services as
it is designed to operate long-running processes without any
user intervention. A service can run continuously in the
background even if the application is closed or the user
switches to another application.
Types of Android Service:
1. Foreground Services:
Services that notify the user about its ongoing operations are
termed as Foreground Services. Users can interact with the
service by the notifications provided about the ongoing task.
Such as in downloading a file, the user can keep track of the
progress in downloading and can also pause and resume the
process.

2. Background Services:
Background services do not require any user intervention.
These services do not notify the user about ongoing
background tasks and users also cannot access them. The
process like schedule syncing of data or storing of data fall
under this service.

3. Bound Services:
This type of android service allows the components of the
application like activity to bound themselves with it. Bound
services perform their task as long as any application
component is bound to it. More than one component is
allowed to bind themselves with a service at a time. In order
to bind an application component with a
service bindService() method is used.

Receiving and Broadcasting Intents


In the context of Android development, intents are a
fundamental messaging system used to enable communication
between different components of an Android application, as
well as between different applications. Intents can be broadly
categorized into two types: explicit intents and implicit
intents. Receiving and broadcasting intents are two
fundamental operations involving intents.
Receiving Intents:
Receiving intents involves listening for and processing intents
sent by other components or applications. Typically, this is
done by components such as activities, services, or broadcast
receivers.
1. Broadcast Receivers: Broadcast receivers are
components in Android applications that respond to broadcast
messages from other applications or from the system itself. To
receive intents, you declare and register a broadcast receiver
in your AndroidManifest.xml file or register it dynamically
within your code. You specify the intent filter to indicate
which intents your broadcast receiver should listen for. When
an intent matching the filter is broadcasted, the onReceive()
method of the broadcast receiver is invoked, allowing you to
handle the intent accordingly.
2. Explicit Intents: Explicit intents are used to launch
specific components within the same application or another
application. When you want to send an intent to a specific
component, you specify the target component's class name
explicitly.
Broadcasting Intents:
Broadcasting intents involves sending messages, or
"broadcasts," to inform other components or applications
about a particular event or state change. There are two types
of broadcasts in Android: system broadcasts and custom
broadcasts.
1. System Broadcasts: System broadcasts are sent by the
Android system to notify all interested applications about
system events such as device boot, network connectivity
changes, battery status changes, etc. These broadcasts are
typically used for system-wide events that may affect multiple
applications.
2. Custom Broadcasts: Custom broadcasts are broadcasts
sent by applications to communicate internally or with other
applications. These broadcasts are defined and sent by
applications according to their specific needs. To send a
custom broadcast, you create an intent with a custom action
string and any additional data needed, and then call
sendBroadcast() or sendOrderedBroadcast() method to
broadcast the intent. Other components or applications can
then register broadcast receivers with matching intent filters to
receive and handle these custom broadcasts.
Example Scenario:
Imagine you have a messaging application. When a new
message is received, you want to notify the user and update
the message list. Here's how you might use receiving and
broadcasting intents in this scenario:
 Receiving Intent: You would register a broadcast
receiver in your messaging application to listen for a custom
intent indicating the arrival of a new message. When a new
message arrives, your broadcast receiver would be triggered,
allowing you to update the message list and display a
notification to the user.
 Broadcasting Intent: When a new message is received,
your messaging application would broadcast a custom intent
indicating the arrival of the new message. Other components
within your application (e.g., the message list UI) or even
other applications (e.g., a widget displaying message count)
that are interested in knowing about new messages could
register broadcast receivers to receive and handle this custom
intent.

Android Manifest file and its common settings


The AndroidManifest.xml file is an essential component of
every Android application. It contains essential information
about the app to the Android system and is required for the
proper functioning of the app. Let's break down its
components and common settings:
1. Package Name:
<manifest package="com.example.myapp">: This attribute
uniquely identifies your app on the device and is used by the
Android system to resolve any conflicts that may arise
between apps.
2. Application Components:
Activities: Activities represent individual screens in your app.
Services: Services perform background tasks without a user
interface.
Broadcast Receivers: Broadcast receivers listen for and
respond to system-wide broadcast announcements.
Content Providers: Content providers manage access to a
structured set of data.
3. Permissions:
<uses-permission>: This tag specifies permissions that the
app needs to access certain features or data on the device, like
internet access, camera, etc.
Example:<uses-permission
android:name="android.permission.INTERNET" />
4. Intent Filters:
<intent-filter>: This tag specifies the intents that an app
component can respond to. It declares what type of intents the
component can handle.
Example (for Activities):
This example specifies that MainActivity is the main entry
point of the app.
5. Application Theme:
android:theme: This attribute specifies the default theme to
be applied to the entire application, including all activities.
Example: android:theme="@style/AppTheme"
6. Activity Metadata:
<meta-data>: This tag can be used to attach additional
metadata to various components of the application.

Example:
7. Debugging Settings:
debuggable: This attribute specifies whether the application
can be debugged. It should be set to false for release builds.
Example: android:debuggable="true"
8. Screen Orientation:
screenOrientation: This attribute specifies the orientation of
the screen for an activity.
Example: android:screenOrientation="portrait"

9. Minimum SDK Version:


minSdkVersion: This attribute specifies the minimum
Android API level required for the application to run.
Example: android:minSdkVersion="21"
10. Target SDK Version:
targetSdkVersion: This attribute specifies the API level that
the app is targeting.
Example: android:targetSdkVersion="31"
11. Versioning:
versionCode: This attribute is an integer value that represents
the version of the application code.
versionName: This attribute is a string value that represents
the version of the application, visible to users.
Example:

12. Hardware and Software Features:


<uses-feature>: This tag specifies hardware and software
features that the app requires.
Example:

Using Intent Filters


The intent is a messaging object which tells what kind of
action to be performed. The intent’s most significant use is
the launching of the activity. Intent facilitates the
communication between the components.
There are two types of intent
1. Explicit intent: Explicit intent can do the specific
application action which is set by the code like changing
activity, In explicit intent user knows about all the things like
after clicking a button which activity will start and Explicit
intents are used for communication inside the application
2. Implicit Intent: Implicit intents do not name a specific
component like explicit intent, instead declare general action
to perform, which allows a component from another app to
handle.
For example: when you tap the share button in any app you
can see the Gmail, Bluetooth, and other sharing app options.
Intent Filter
 Implicit intent uses the intent filter to serve the user
request.
 The intent filter specifies the types of intents that an
activity, service, or broadcast receiver can respond.
 Intent filters are declared in the Android manifest file.
 Intent filter must contain <action>
Most of the intent filter are describe by its
1. <action>,
2. <category> and
3. <data>.
1. <action>
Syntax: <action android:name="string" />
Adds an action to an intent filter. An <intent-filter> element
must contain one or more <action> elements. If there are
no <action> elements in an intent filter, the filter doesn’t
accept any Intent objects.
Examples of common action:
 ACTION_VIEW: Use this action in intent
with startActivity() when you have some information that
activity can show to the user like showing an image in a
gallery app or an address to view in a map app
 ACTION_SEND: You should use this in intent
with startActivity() when you have some data that the user
can share through another app, such as an email app or social
sharing app.
2. <category>
Syntax:
<category android:name="string" />
Adds a category name to an intent filter. A string containing
additional information about the kind of component that
should handle the intent.
Example of common categories:
 CATEGORY_BROWSABLE: The target activity
allows itself to be started by a web browser to display data
referenced by a link.

3. <data>
Syntax:

<data android:scheme="string"
android:host="string"
android:port="string"
android:path="string"
android:pathPattern="string"
android:pathPrefix="string"
android:mimeType="string" />

Adds a data specification to an intent filter. The specification


can be just a data type, just a URI, or both a data type and a
URI.
/* Refer online for example to explaination in detail*/

You might also like