Application Fundamentals
Android Apps are written in Java. The Android SDK tools
compile the code, data and resource files into an APK (Android package).
Generally, one APK file contains all the content of the app.
Each app runs on its own virtual machine(VM) so that app
can run isolated from other apps. Android system works with the principle of
least privilege. Each app only has access to the components which it requires
to do its work, and no more. However, there are ways for an app to share data
with other apps, such as by sharing Linux user id
between app, or apps can request permission to access
device data like SD card, contacts etc.
App Components
App components are the building blocks of an Android app.
Each components plays a specific role in an Android app which serves a distinct
purpose and has distinct life-cycles(the flow of how and when the component is
created and destroyed). Here are the four types of app components:
1.
Activities: An
activity represents a single screen with a User Interface(UI). An Android app
may have more than one activity. (e.g. an email app might have one activity to
list all the emails, another to show the contents of each email, and another to
compose new email.) All the activities in an App work together to create a User
eXperience (UX).
2. Services: A
service runs in the background to perform long-running operations or to perform
work for a remote processes. A service does not provide any UI, it runs only in
the background with the User's input.
(e.g. a service can play music in the background while
the user is in a different App, or it might download data from the internet
without blocking user's interaction with the Android device.)
3. Content Providers: A content provider manages shared app data. There are
four ways to store data in an app: it can be written to a file and stored in
the file system, inserted or updated to a SQLite database, posted
to the web, or saved in any other persistent storage
location the App can access. Through content providers, other Apps can query or
even modify the data. (e.g. Android system provides a content provider that manages
the user's contact information so that any app which has permission can query
the contacts.) Content providers can also be used to save the data which is
private to the app for better data integrity.
4. Broadcast receivers: A broadcast receiver responds to the system-wide broadcasts
of announcements (e.g. a broadcast announcing that the screen has turned off,
the battery is low, etc.) or from Apps (e.g. to let other
apps know that some data has been downloaded to the
device and is available for them to use). Broadcast receivers don't have UIs
but they can show notification in the status bar to alert the user. Usually
broadcast receivers are used as a gateway to other components of the app,
consisting mostly of activities and services.
One unique aspect of the Android system is that any app
can start another app's component (e.g. if you want to make call, send SMS,
open a web page, or view a photo, there is an app which already does that and
your app can make use of it, instead of developing a new activity for the same
task).
When the system starts a component, it starts the process
for that app (if it isn't already running, i.e. only one foreground process per
app can run at any given time on an Android system) and instantiates the
classes needed for that component. Thus the component runs on the process of
that App that it belongs to. Therefore, unlike apps
on other systems, Android apps don't have a single entry
point(there is no main() method).
Because the system runs each app in a separate process, one app cannot directly
activate another app's components, however the Android system can. Thus to
start another app's component, one app must send a message to the system that
specifies an intent to start that component, then the system will start that
component.
Context
Instances of the class android.content.Context provide the connection to the Android system which
executes the application. Instance of Context is required to get access to the
resources of the project and the global information
about the app's environment.
Let's have an easy to digest example: Consider you are in a hotel,
and you want to eat something. You call roomservice and ask them to bring you
things or clean up things for you. Now think of this hotel as an Android app,
yourself as an activity, and the room-service person is then your
context, which provides you access to the hotel resources like room-service,
food items etc.
Yet an other example, You are in a restaurant sitting on a table,
each table has an attendant, when ever you want to order food items you ask the
attendant to do so. The attendant then places your order and your food items
gets served on your table. Again in this example, the restaurant is an Android
App, the tables or the customers are App
components, the food items are your App resources and the attendant
is your context thus giving you a way to access the resources like food items.
Activating any of the above components requires the context's
instance. Not just only the above, but almost every system resource: creation
of the UI using views(discussed later), creating instance of system services,
starting new activities or services -- all require context.
Android Application Fundamental
Reviewed by #
on
January 29, 2019
Rating:
No comments: