Android apps are written in the
Java programming language. The Android SDK tools compile your code—along with
any data and resource files—into an APK: an Android package, which is an
archive file with an .apk
suffix. One APK file contains all the contents of an Android app and is the
file that Android-powered devices use to install the app.
Once
installed on a device, each Android app lives in its own security sandbox:
·
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 (the ID 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. Android starts the process when any of
the app's components need to be executed, then shuts down the process when it's
no longer needed or when the system must recover memory for other apps.
·
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 user's contacts, SMS
messages, the mountable storage (SD card), camera, Bluetooth, and more. All app
permissions must be granted by the user at install time.
·
The core
framework components that define your app.
·
The
manifest file in which you declare components and required device features for
your app.
·
Resources
that are separate from the app code and allow your app to gracefully optimize
its behavior for a variety of device configurations.
Android Application Building Blocks
App components are the essential
building blocks of an Android app. Each component is a different point through
which the system can enter your app. Not all components are actual entry points
for the user and some depend on each other, but each one exists as its own
entity and plays a specific role—each one is a unique building block that helps
define your app's overall behavior.
There are 4 building blocks in
android:
1.
Activity
2.
Service
3.
Content Provider
4.
Broadcast Receiver
Activity:
An activity 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. As such, a different app can start any one of these activities (if the email app allows it). For
example, a camera app
can start the activity in the email app that composes new mail, in order for
the user to share a picture.
Services:
A service is a component that runs in
the background to perform long-running operations or to perform work for remote
processes. A service does not provide a user interface. 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 in order to interact with it.
Content providers:
A content provider manages a shared
set of app data. You can store the data in the file system, SQLite database, on the web, or any other
persistent storage location your app can access. Through the content provider,
other apps can query or even modify the data (if the content provider allows it). For
example, the Android system provides a content provider that manages the user's
contact information. As such, any app with the proper permissions can query
part of the content provider (such
as 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. For
example, the Note Pad sample
app uses a content provider to save notes.
Broadcast receiver:
A broadcast receiver is a component
that responds to system-wide broadcast announcements. Many broadcasts originate
from the system—for example, a broadcast announcing that the screen has turned
off, the battery is low, or a picture was captured. Apps can also initiate
broadcasts—for example, to let other apps know that some data has been
downloaded to the device and is available for them to use. Although broadcast
receivers don't display a user interface, they may 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. For instance, it might
initiate a service to perform some work based on the event.
Activating Components
Three of the four component
types—activities, services, and broadcast receivers—are activated by an
asynchronous message called an intent. Intents bind individual components to
each other at runtime (you can think of them as the messengers that request an
action from other components), whether the component belongs to your app or
another.
An intent is created with an Intent object, which defines a message to
activate either a specific component or a specific type of component—an intent
can be either explicit or implicit, respectively.
For activities and services, an
intent defines the action to perform (for example, to "view" or
"send" something) and may specify the URI of the data to act on
(among other things that the component being started might need to know). For
example, an intent might convey a request for an activity to show an image or
to open a web page. In some cases, you can start an activity to receive a
result, in which case, the activity also returns the result in an Intent (for example, you can issue an intent to
let the user pick a personal contact and have it returned to you—the return
intent includes a URI pointing to the chosen contact).
For broadcast receivers, the intent
simply defines the announcement being broadcast (for example, a broadcast to
indicate the device battery is low includes only a known action string that
indicates "battery is low").
The other component type, content
provider, is not activated by intents. Rather, it is activated when targeted by
a request from a ContentResolver. The
content resolver handles all direct transactions with the content provider so
that the component that's performing transactions with the provider doesn't
need to and instead calls methods on the ContentResolver
object. This leaves a layer of abstraction between the content provider and the
component requesting information (for security).
There
are separate methods for activating each type of component:
·
You can
start an activity (or give it something new to do) by passing an Intent to startActivity()
or startActivityForResult() (when you want the activity to return a result).
·
You can
start a service (or give new instructions to an ongoing service) by passing an Intent to startService().
Or you can bind to the service by passing an Intent to bindService().
·
You can
initiate a broadcast by passing an Intent to methods like sendBroadcast(), sendOrderedBroadcast(), or
sendStickyBroadcast().
·
You can
perform a query to a content provider by calling query() on a ContentResolver.
Shutting down Components
There
are separate methods for shutting down each type of component:
·
You can stop
an activity by calling finish() (Call this when your activity is done and should be
closed.)or finishActivity() (Force finish another activity that you had previously
started with startActivityForResult()).
·
You can stop
a service by calling stopSelf() (Stop the service, if it was previously started.) or stopSelfResult() (Be
careful about ordering of your calls to this function.. If you call this
function with the most-recently received ID before you have called it for
previously received IDs, the service will be immediately stopped anyway) or Context.stopService() (Request that a given application service be stopped.
If the service is not running, nothing happens. Otherwise it is stopped. Note
that calls to startService() are not counted -- this stops the service no matter
how many times it was started) or Context.unbindService()
(Disconnect from an application service.
You will no longer receive calls as the service is restarted, and the service
is now allowed to stop at any time).
·
You can unregister
a BroadcastReceiver by calling Context. unregisterReceiver()
(Unregister a previously registered BroadcastReceiver.
All filters that have been registered for this BroadcastReceiver
will be removed).
No comments:
Post a Comment