#4 Android Application Fundamental
#4 Android Application Fundamental
Android apps can be written using Kotlin and the Java programming language, and
C++ languages. The Android SDK tools compile your code along with any data and
resource files into an APK or an Android App Bundle.
An Android package, which is an archive file with an .apk suffix, contains the
contents of an Android app required at runtime, and it is the file that Android-
powered devices use to install the app.
An Android App Bundle, which is an archive file with an .aab suffix, contains the
contents of an Android app project, including some additional metadata that isn't
required at runtime. An AAB is a publishing format and can't be installed on
Android devices. It defers APK generation and signing to a later stage.
When distributing your app through Google Play, for example, Google Play's
servers generate optimized APKs that contain only the resources and code that
are required by the particular device requesting installation of the app.
Each Android app lives in its own security sandbox, protected by the following
Android security features:
➢ The Android operating system is a multi-user Linux system in which each
app is a different user.
➢ By default, the system assigns each app a unique Linux user ID, which is
used only by the system and is unknown to the app. The system sets
permissions for all the files in an app so that only the user ID assigned to
that app can access them.
➢ Each process has its own virtual machine (VM), so an app's code runs in
isolation from other apps.
➢ By default, every app runs in its own Linux process. The Android system
starts the process when any of the app's components need to be
executed, and then shuts down the process when it's no longer needed or
when the system must recover memory for other apps.
The Android system implements the principle of least privilege. That is, each
app, by default, has access only to the components that it requires to do its
work and no more. This creates a very secure environment in which an app can't
access parts of the system it is not given permission for.
However, there are ways for an app to share data with other apps and for an app
to access system services:
➢ It's possible to arrange for two apps to share the same Linux user ID, in
which case they are able to access each other's files. To conserve system
resources, apps with the same user ID can also arrange to run in the same
Linux process and share the same VM. The apps must also be signed with
the same certificate.
➢ An app can request permission to access device data such as the device's
location, camera, and Bluetooth connection. The user has to explicitly
grant these permissions. For more information about permissions,
see Permissions on Android.
App components
App components are the essential building blocks of an Android app. Each
component is an entry point through which the system or a user can enter your
app. Some components depend on others.
➢ Activities
➢ Services
➢ Broadcast receivers
➢ Content providers
Each type serves a distinct purpose and has a distinct lifecycle that defines how
a component is created and destroyed.
Activities
An activity is the entry point for interacting with the user. It represents a
single screen with a user interface. For example, an email app might have
one activity that shows a list of new emails, another activity to compose
an email, and another activity for reading emails. Although the activities
work together to form a cohesive user experience in the email app, each
one is independent of the others.
A different app can start any one of these activities if the email app allows
it. For example, a camera app might start the activity in the email app for
composing a new email to let the user share a picture.
For example, a service might play music in the background while the user
is in a different app, or it might fetch data over the network without
blocking user interaction with an activity. Another component, such as an
activity, can start the service and let it run or bind to it to interact with it.
There are two types of services that tell the system how to manage an app:
started services and bound services.
1. Started services tell the system to keep them running until their work is
completed. This might be to sync some data in the background or play
music even after the user leaves the app. Syncing data in the background
or playing music represent different types of started services, which the
system handles differently:
➢ Music playback is something the user is directly aware of, and the
app communicates this to the system by indicating that it wants to
be in the foreground, with a notification to tell the user that it is
running. In this case, the system prioritizes keeping that service's
process running, because the user has a bad experience if it goes
away.
➢ A regular background service is not something the user is directly
aware of, so the system has more freedom in managing its process.
It might let it be killed, restarting the service sometime later, if it
needs RAM for things that are of more immediate concern to the
user.
2. Bound services run because some other app (or the system) has said that
it wants to make use of the service. A bound service provides an API to
another process, and the system knows there is a dependency between
these processes. So if process A is bound to a service in process B, the
system knows that it needs to keep process B and its service running for A.
Because of their flexibility, services are useful building blocks for all kinds
of higher-level system concepts. Live wallpapers, notification listeners,
screen savers, input methods, accessibility services, and many other core
system features are all built as services that applications implement and
the system binds to when they run.
Broadcast receivers
A broadcast receiver is a component that lets the system deliver events to
the app outside of a regular user flow so the app can respond to system-
wide broadcast announcements. Because broadcast receivers are another
well-defined entry into the app, the system can deliver broadcasts even to
apps that aren't currently running.
So, for example, an app can schedule an alarm to post a notification to tell
the user about an upcoming event. Because the alarm is delivered to
a BroadcastReceiver in the app, there is no need for the app to remain
running until the alarm goes off.
Although broadcast receivers don't display a user interface, they can create
a status bar notification to alert the user when a broadcast event occurs.
More commonly, though, a broadcast receiver is just a gateway to other
components and is intended to do a very minimal amount of work.
Content providers
A content provider manages a shared set of app data that you can store in
the file system, in a SQLite database, on the web, or on any other persistent
storage location that your app can access. Through the content provider,
other apps can query or modify the data, if the content provider permits it.
For example, the Android system provides a content provider that manages
the user's contact information. Any app with the proper permissions can
query the content provider, such as using ContactsContract.Data, to read
and write information about a particular person.
Content providers are also useful for reading and writing data that is private
to your app and not shared.
Android API
The Android API (Application Programming Interface) is a set of rules,
protocols, and tools for building software applications on the Android
platform. It provides a way for developers to interact with the underlying
Android operating system and access various functionalities, such as
hardware sensors, network services, and user interface components.
Each API level introduces new features, improvements, and changes to the
Android platform, and developers can target specific API levels based on
their application's requirements and the features they want to utilize. This
helps ensure that apps remain compatible with a wide range of Android
devices while taking advantage of the latest capabilities offered by the
platform. https://developer.android.com/tools/releases/platforms