AndroidManifest.xml
The components and settings of an Android
application are described in the AndroidManifest.xml file. For
example allactivities and services of the
application must be declared in this file.
It must also contain the required permissions for
the application. For example if the application requires network access it must
be specified here.
<?xml
version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.vogella.android.temperature"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Convert"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="9" />
</manifest>
The package attribute defines the
base package for the Java objects referred to in this file. If a Java object
lies within a different package, it must be declared with the full qualified
package name.
Google Play requires that every Android application
uses its own unique package. Therefore it is a good habit to use your reverse
domain name as package name. This will avoid collisions with other Android
applications.
android:versionName and android:versionCode specify
the version of your application. versionName is what the user
sees and can be any String.
versionCode must
be an integer. The Android Market determine based on the versionCode, if
it should perform an update of the applications for the existing installations.
You typically start with "1" and increase this value by one, if you
roll-out a new version of your application.
The <activity> tag defines
an activity, in this example pointing to the Convert class
in thede.vogella.android.temperature package. An intent filter is
registered for this class which defines that thisactivity is
started once the application starts (action android:name="android.intent.action.MAIN" ).
The category definition category android:name="android.intent.category.LAUNCHER" defines
that this application is added to the application directory on the Android
device.
The @string/app_name value refers to
resource files which contain the actual value of the application name. The
usage of resource file makes it easy to provide different resources, e.g.
strings, colors, icons, for different devices and makes it easy to translate
applications.
The uses-sdk part of the AndroidManifest.xml file
defines the minimal SDK version for which your application is valid. This will
prevent your application being installed on unsupported devices.
Activities and Lifecycle
The Android system controls the lifecycle of your
application. At any time the Android system may stop or destroy your
application, e.g. because of an incoming call. The Android system defines a
lifecycle for activities via predefined methods. The most
important methods are:
·
onSaveInstanceState() - called
after the Activity is stopped. Used to save data so that the Activitycan
restore its states if re-started
·
onPause() - always called if
the Activity ends, can be used to release resource or save data
·
onResume() - called if the Activity is
re-started, can be used to initialize fields
Configuration Change
An Activity will also be restarted, if a
so called "configuration change" happens. A configuration change
happens if an event is triggered which may be relevant for the application. For
example if the user changes the orientation of the device (vertically or
horizontally). Android assumes that an Activity might want to use
different resources for these orientations and restarts the Activity.
In the emulator you can simulate the change of the
orientation via Ctrl+F11.
You can avoid a restart of your application for
certain configuration changes via the configChanges attribute on yourActivity definition
in your AndroidManifest.xml. The following Activity will not be
restarted in case of orientation changes or position of the physical keyboard
(hidden / visible).
<activity android:name=".ProgressTestActivity"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden|keyboard">
</activity>
Context
The class android.content.Context provides
the connection to the Android system and the resources of the project. It is
the interface to global information about the application environment.
The Context also provides access
to Android services, e.g. the Location Service.
activities and services extend
the Context class.
0 comments:
Post a Comment