You are on page 1of 33

Understanding the

Anatomy of Android
Application
Important Android Terminology
• Context: *central command center for an Android application.
*All application-specific functionality can be accessed through
the context.

• Activity: *An Android application is a collection of tasks, each of which is


called an Activity.
*Each Activity within an application has a unique task or purpose.

• Intent: *The OS uses an asynchronous messaging mechanism to match


task requests with the appropriate Activity.
*Each request is packaged as an Intent. Each such request as a
message stating an intent to do something.

• Service: *Tasks that do not require user interaction can be encapsulated


in a service.
*A service is most useful when the operations are lengthy (offloading
time-consuming processing) or need to be done regularly (such as
checking a server for new mail).
• Context:
*central command center for an
Android application.
*All application-specific functionality
can be accessed through the context.
Using the Application Context
• The central location for all top-level
application functionality.
• The Context class can be used to manage
application-specific configuration details as
well as application-wide operations and data
• to access settings and resources shared across
multiple Activity instances
Retrieving the Application Context
• retrieve the Context for the current process
using the getApplicationContext() method:
Context context getApplicationContext();
Using the Application Context:
• After the retrieval of a valid application
Context, it can be used to access application
wide features and services.
Retrieving Application Resources
• retrieve application resources using the
getResources() method of the application
Context.
• to retrieve a resource by using its resource
identifier, a unique number automatically
generated within the R.java class.
• The following example retrieves a String instance
from the application resources by its resource ID:
String greeting
getResources().getString(R.string.hello);
Accessing Application Preferences

• Retrieval of shared application preferences


using the getSharedPreferences() method of
the application Context.
• The SharedPreferences class can be used to
save simple application data, such as
configuration settings.
Accessing Other Application
Functionality Using Context
• The application Context provides access to a
number of other top-level application features.
• Application Functionality Using Context :-
1. Launch Activity instances
2. Retrieve assets packaged with the application
3. Request a system service (for example, location
service)
4. Manage private application files, directories, and
databases
5. Inspect and enforce application permissions
• Activity:
*An Android application is a
collection of tasks, each of which is
called an Activity.
*Each Activity within an application
has a unique task or purpose.
Performing Application Tasks with Activities
• The Android Activity class
(android.app.Activity) is core to any Android
application.
Using Activity Callbacks to Manage
Application State and Resources
• Different important state changes within the Activity
lifecycle are punctuated by a series of important
method
• The method stubs for the most important callbacks of
the Activity class:
public class MyActivity extends Activity {
protected void onCreate(Bundle savedInstanceState);
protected void onStart();
protected void onRestart();
protected void onResume();
protected void onPause();
protected void onStop();
protected void onDestroy();
}
Defining Your Application Using
the Android Manifest File
1. Configuring the Android Manifest File
2. Managing Your Application’s Identity
3. Enforcing Application System Requirements
4. Registering Activities and Other Application
Components
5. Working with Permissions
6. Exploring Other Manifest File Settings
1. Configuring the Android Manifest File
• Install and upgrade the application package.
• Display the application details such as the application name, description,
and icon to users.
• Specify application system requirements, including which Android SDKs
are supported, what hardware configurations are required (for example, d-
pad navigation), and which platform features the application relies upon
(for example, uses multitouch
capabilities).
• Launch application activities.
• Manage application permissions.
• Configure other advanced application configuration details, including
acting as a
service, broadcast receiver, or content provider.
• Enable application settings such as debugging and configuring
instrumentation for
application testing.
Editing the Android Manifest File
Using Eclipse
• The Eclipse Manifest File resource editor
organizes the manifest information into
categories:
1. The Manifest tab
2. The Application tab
3. The Permissions tab
4. The Instrumentation tab
5. The AndroidManifest.xml tab
Configuring Package-Wide Settings Using the Manifest
Tab
• The Manifest tab contains package-wide settings,
including the package
• name, version information, and supported Android SDK
information.
Managing Application and Activity Settings Using the
Application Tab
• The Application tab contains application-wide settings,
including the application label and icon,
• information about the application components such
as activities, intent filters, and other application
components, including configuration for services,
intent filters, and content providers.
Enforcing Application Permissions Using
the Permissions Tab
• The Permissions tab contains any permission
rules required by the application.
• This tab can also be used to enforce custom
permissions created for the application.
Managing Test Instrumentation Using the
Instrumentation Tab
• The Instrumentation tab allows the developer
to declare any instrumentation classes for
monitoring the application.
Editing the Manifest File Manually

• The Android manifest file is a specially


formatted XML file and can be edited XML
manually by clicking on the
AndroidManifest.xml tab.
• Android manifest files generally include a
single <manifest> tag with a single
<application> tag.
The following is a sample AndroidManifest.xml file for
an application called Multimedia
Managing Application’s Identity
• Versioning Your Application
• <manifest xmlns:android
"http://schemas.android.com/apk/res/android"
package "com.androidbook.multimedia"
android:versionCode "1“
android:versionName "1.0">
Setting the Application Name and Icon
• <application android:icon "@drawable/icon"
android:label "@string/app_name">
Enforcing Application System
Requirements
• Some of the application system requirements that
developers can configure through the Android manifest
file include
1. The Android SDK versions supported by the
application
2. The Android platform features used by the
application
3. The Android hardware configurations required by the
application
4. The screen sizes and pixel densities supported by the
application
5. Any external libraries that the application links to
Targeting Specific SDK Versions

• Developers can specify which versions of the Android


platform an application supports within its Android
manifest file using the <uses-sdk> tag.
This tag has three important attributes:
• The minSdkVersion attribute:This attribute specifies
the lowest API level that the application supports.
<uses-sdk android:minSdkVersion "4" />
• The targetSdkVersion attribute:This attribute specifies
the optimum API level that the application supports.
<uses-sdk android:minSdkVersion "4"
android:targetSdkVersion "8" />
• The maxSdkVersion attribute:This attribute specifies
the highest API level that the application supports.
Enforcing Application Platform
Requirements
• Specifying Supported Input Methods
<uses-configuration android:reqHardKeyboard
"true“ android:reqTouchScreen "finger" />
<uses-configuration android:reqHardKeyboard
"true“ android:reqTouchScreen "stylus" />
Specifying Required Device Features
an application that requires both a light and
proximity sensor requires two tags:
• <uses-feature android:name
"android.hardware.sensor.light" />
• <uses-feature android:name
"android.hardware.sensor.proximity" />
Specifying Supported Screen Sizes
<supports-screens android:smallScreens "true"
android:normalScreens "true"
android:largeScreens"false"
android:anyDensity "true"/>
Working with External Libraries
<uses-library android:name
"com.sharedlibrary.sharedStuff" />
Registering Activities and Other Application
Components
<activity android:name "AudioActivity" />
OR
<activity android:name ".AudioActivity" />
OR
<activity android:name
"com.androidbook.multimedia.AudioActivity"
/>
Designating a Primary Entry Point Activity
for Your Application
Using an Intent Filter
<activity android:name ".MultimediaMenuActivity"
android:label "@string/app_name">
<intent-filter>
<action android:name
"android.intent.action.MAIN" />
<category android:name
"android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Configuring Other Intent Filters
<intent-filter>
<action android:name "android.intent.action.VIEW"
/>
<category android:name
"android.intent.category.BROWSABLE" />
<category android:name
"android.intent.category.DEFAULT" />
<data android:scheme "geoname"/>
</intent-filter>
Registering Services and Broadcast
Receivers

• Services are registered using the <service> tag.


• Broadcast Receivers are registered using the
<receiver> tag.
• Both Services and Broadcast Receivers use
intent filters.
Working with Permissions
• Registering Permissions Application Requires
• <uses-permission android:name
"android.permission.CAMERA" />
• Registering Permissions Application Grants to Other
Applications
>>> Permissions can be enforced at several points:
• When starting an Activity or Service
• When accessing data provided by a content provider
• At the function call level
• When sending or receiving broadcasts by an Intent

You might also like