android unit2
android unit2
Applications
Working of Android Applications
• WHAT MAKES AN ANDROID APPLICATION?
• Android applications consist of loosely coupled components, bound
by the application manifest that describes each component and how
they interact.
• The manifest is also used to specify the application’s metadata, its
hardware and platform requirements, external libraries, and required
permissions.
• The following components comprise the building blocks for all your
Android applications:
• ‰ Activities — Your application’s presentation layer. The UI of your
application is built around one or more extensions of the Activity class.
Activities use Fragments and Views to layout and display information,
and to respond to user actions. Compared to desktop development,
Activities are equivalent to Forms.
• ‰ Services — The invisible workers of your application. Service
components run without a UI, updating your data sources and
Activities, triggering Notifications, and broadcasting Intents. They’re
used to perform long running tasks, or those that require no user
interaction (such as network lookups or tasks that need to continue
even when your application’s Activities aren’t active or visible.)
• ‰ Content Providers — Shareable persistent data storage. Content
Providers manage and persist application data and typically interact with
SQL databases. They’re also the preferred means to share data across
application boundaries. You can configure your application’s Content
Providers to allow access from other applications, and you can access the
Content Providers exposed by others. Android devices include several
native Content Providers that expose useful databases such as the media
store and contacts.
• ‰ Intents — A powerful interapplication message-passing framework.
Intents are used extensively throughout Android. You can use Intents to
start and stop Activities and Services, to broadcast messages system-
wide or to an explicit Activity, Service, or Broadcast Receiver, or to
request an action be performed on a particular piece of data.
• ‰ Broadcast Receivers — Intent listeners. Broadcast Receivers enable your
application to listen for Intents that match the criteria you specify. Broadcast
Receivers start your application to react to any received Intent, making them
perfect for creating event-driven applications.
• ‰ Widgets — Visual application components that are typically added to the
device home screen. A special variation of a Broadcast Receiver, widgets
enable you to create dynamic, interactive application components for users to
embed on their home screens.
• Notifications — Notifications enable you to alert users to application events
without stealing focus or interrupting their current Activity. They’re the
preferred technique for getting a user’s attention when your application is not
visible or active, particularly from within a Service or Broadcast Receiver. For
example, when a device receives a text message or an email, the messaging
and Gmail applications use Notifications to alert you by flashing lights, playing
sounds, displaying icons, and scrolling a text summary. You can trigger these
notifications from your applications.
INTRODUCING THE APPLICATION MANIFEST
FILE
• Each Android project includes a manifest fi le, AndroidManifest.xml, stored in the
root of its project hierarchy. The manifest defines the structure and metadata of
your application, its components, and its requirements.
• It includes nodes for each of the Activities, Services, Content Providers, and
Broadcast Receivers that make up your application and, using Intent Filters and
Permissions, determines how they interact with each other and with other
applications.
• The manifest can also specify application metadata (such as its icon, version
number, or theme), and additional top-level nodes can specify any required
permissions, unit tests, and define hardware, screen, or platform requirements
(as described next).
• The manifest is made up of a root manifest tag with a package attribute set to the
project’s package. It should also include an xmlns:android attribute that supplies
several system attributes used within the file.
• Use the versionCode attribute to define the current application version
as an integer that increases with each version iteration, and use the
versionName attribute to specify a public version that will be displayed
to users.
• You can also specify whether to allow (or prefer) for your application be
installed on external storage (usually an SD card) rather than internal
storage using the installLocation attribute. To do this specify either
preferExternal or auto, where the former installs to external storage
whenever possible, and the latter asks the system to decide.
A Closer Look at the Application Manifest
• The following XML snippet shows a typical manifest node:
• <manifest
xmlns:android=”http://schemas.android.com/apk/res/android”
• package=“com.paad.myapp“
• android:versionCode=“1“
• android:versionName=“0.9 Beta“
• android:installLocation=“preferExternal“>
• [ ... manifest nodes ... ]
• </manifest>
• The manifest tag can include nodes that define the application
components, security settings, test classes, and requirements that make
up your application.
• The following list gives a summary of the available manifest sub-node
tags and provides an XML snippet demonstrating how each tag is used:
• <uses-sdk android:minSdkVersion=”6”
• android:targetSdkVersion=”15”/>
• ‰ uses-configuration — The uses-configuration nodes specify each combination of
input mechanisms are supported by your application. You shouldn’t normally need
to include this node, though it can be useful for games that require particular input
controls.
• You can specify any combination of input devices that include the following:
• ‰ reqFiveWayNav — Specify true for this attribute if you require an input device
capable of navigating up, down, left, and right and of clicking the current selection.
This includes both trackballs and directional pads (D-pads).
• ‰ reqHardKeyboard — If your application requires a hardware keyboard, specify
true.
• ‰ reqKeyboardType — Lets you specify the keyboard type as one of nokeys, qwerty,
twelvekey, or undefined.
• ‰ reqNavigation — Specify the attribute value as one of nonav, dpad, trackball,
wheel, or undefined as a required navigation device.
• ‰ reqTouchScreen — Select one of notouch, stylus, finger, or undefined to specify
the required touchscreen input.
• You can specify multiple supported configurations, for example, a device
with a finger touchscreen, a trackball, and either a QUERTY or a twelve-
key hardware keyboard, as shown here:
• <uses-configuration android:reqTouchScreen=”finger”
• android:reqNavigation=”trackball”
• android:reqHardKeyboard=”true”
• android:reqKeyboardType=”qwerty”/>
• <uses-configuration android:reqTouchScreen=”finger”
• android:reqNavigation=”trackball”
• android:reqHardKeyboard=”true”
• android:reqKeyboardType=”twelvekey”/>
• ‰ uses-feature — Android is available on a wide variety of hardware
platforms. Use multiple uses-feature nodes to specify which hardware
features your application requires.
• This prevents your application from being installed on a device that does not
include a required piece of hardware, such as NFC hardware, as follows:
• <uses-feature android:name=”android.hardware.nfc” />
• You can require support for any hardware that is optional on a compatible
device. Currently, optional hardware features include the following:
• ‰ Audio — For applications that requires a low-latency audio pipeline. Note
that at the time of writing this book, no Android devices satisfied this
requirement.
• ‰ Bluetooth — Where a Bluetooth radio is required.
• ‰ Camera — For applications that require a camera. You can also require (or
set as options) autofocus, flash, or a front-facing camera.
• ‰ Location — If you require location-based services. You can also specify
either network or GPS support explicitly.
• ‰ Microphone — For applications that require audio input.
• ‰ NFC — Requires NFC (near-field communications) support.
• ‰ Sensors — Enables you to specify a requirement for any of the potentially
available hardware sensors.
• ‰ Telephony — Specify that either telephony in general, or a specifi c
telephony radio (GSM or CDMA) is required.
• ‰ Touchscreen — To specify the type of touch-screen your application
requires.
• ‰ USB — For applications that require either USB host or accessory mode
support.
• ‰ Wi-Fi — Where Wi-Fi networking support is required.
• As the variety of platforms on which Android is available increases, so too will
the optional hardware. You can find a full list of uses-feature hardware at
• http://developer.android.com/guide/topics/manifest/uses-feature-element.
html#features-reference.
• The application node also acts as a container for the Activity, Service, Content
Provider, and Broadcast Receiver nodes that specify the application components.
• <application android:icon=”@drawable/icon”
• android:logo=”@drawable/logo”
• android:theme=”@android:style/Theme.Light”
• android:name=”.MyApplicationClass”
• android:debuggable=”true”>
• [ ... application nodes ... ]
• </application>
• ‰ activity — An activity tag is required for every Activity within your
application. Use the android:name attribute to specify the Activity class
name. You must include the main launch Activity and any other Activity
that may be displayed. Trying to start an Activity that’s not defined in
the manifest will throw a runtime exception. Each Activity node
supports intent-filter child tags that define the Intents that can be used
to start the Activity.
• <activity android:name=”.MyActivity”
android:label=”@string/app_name”>
• <intent-filter>
• <action android:name=”android.intent.action.MAIN” />
• <category android:name=»android.intent.category.LAUNCHER» />
• </intent-filter>
• </activity>
• ‰ service — As with the activity tag, add a service tag for each Service
class used in your application. Service tags also support intent-filter
child tags to allow late runtime binding.
• <service android:name=”.MyService”>
• </service>
• Layouts
• Layout resources enable you to decouple your presentation layer from your
business logic by designing UI layouts in XML rather than constructing them in
code.
• You can use layouts to define the UI for any visual component, including
Activities, Fragments, and Widgets.
• Once defined in XML, the layout must be “inflated” into the user interface.
Within an Activity this is done using setContentView (usually within the
onCreate method)
• Fragment Views are inflated using the inflate method from the Inflator object
passed in to the Fragment’s onCreateView handler.
• Menus
• Create menu resources to design your menu layouts in XML, rather than
constructing them in code.
• You can use menu resources to define both Activity and context menus within your
applications, and provide the same options you would have when constructing your
menus in code.
• When defined in XML, a menu is inflated within your application via the inflate
method of the MenuInflator Service, usually within the onCreateOptionsMenu
method.
• Each menu definition is stored in a separate file, each containing a single menu, in
the res/menu folder — the filename then becomes the resource identifier.
Using Resources in Code
• Access resources in code using the static R class. R is a generated class
based on your external resources, and created when your project is
compiled. The R class contains static subclasses for each of the
resource types for which you’ve defined at least one resource. For
example, the default new project includes the R.string and
R.drawable subclasses.
• ‰ Fragments — Fragments, introduced in Android 3.0 (API level 11), are used
to encapsulate portions of your UI. This encapsulation makes Fragments
particularly useful when optimizing your UI layouts for different screen sizes
and creating reusable UI elements. Each Fragment includes its own UI layout
and receives the related input events but is tightly bound to the Activity into
which each must be embedded. Fragments are similar to UI View Controllers
in iPhone development.
• If you prefer the more traditional approach, you can construct the UI in
code:
• @Override
• public void onCreate(Bundle savedInstanceState) {
• super.onCreate(savedInstanceState);
• TextView myTextView = new TextView(this);
• setContentView(myTextView);
• myTextView.setText(“Hello, Android”);
•}
• The setContentView method accepts a single View instance; as a result, you
use layouts to add multiple controls to your Activity.