1、 Android Application Architecture author: Lars Vogel 1、 AndroidManifest.xml The components and settings of an Android application are described in the file AndroidManifest.xml. For example all Activities and Services of the application must be declared in this file. It must also contain the required
2、 permissions for the application. For example if the application requires network access it must be specified here. 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 qualif
3、ied 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
4、 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
5、by one, if you roll-out a new version of your application. The tag defines an Activity, in this example pointing to the Convert class in the de.vogella.android.temperature package. An intent filter is registered for this class which defines that this Activity is started once the application starts (
6、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
7、 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 applicat
8、ion is valid. This will prevent your application being installed on devices with older SDK versions. 2、 R.java and Resources The gen directory in an Android project contains generated values. R.java is a generated class which contains references to certain resources of the project. These resources m
9、ust be defined in the res directory and can be XML files, icons or pictures. You can for example define values, menus, layouts or animations via XML files. If you create a new resource, the corresponding reference is automatically created in R.java via the Eclipse ADT tools. These references are sta
10、tic int values and define IDs for the resources. The Android system provides methods to access the corresponding resource via these IDs. For example to access a String with the R.string.yourString ID, you would use the getString(R.string.yourString) method. R.java is automatically created by the Ecl
11、ipse development environment, manual changes are not necessary and will be overridden by the tooling. 3、 Assets While the res directory contains structured values which are known to the Android platform, the assets directory can be used to store any kind of data. You access this data via the AssetsM
12、anager which you can access the getAssets() method. AssetsManager allows to read an assets as InputStream with the open() method. / Get the AssetManager AssetManager manager = getAssets(); / Read a Bitmap from Assets try InputStream open = manager.open(logo.png); Bitmap bitmap = BitmapFactory.decode
13、Stream(open); / Assign the bitmap to an ImageView in this layout ImageView view = (ImageView) findViewById(R.id.imageView1); view.setImageBitmap(bitmap); catch (IOException e) e.printStackTrace(); 4、 Activities and Layouts The user interface for Activities is defined via layouts. The layout defines
14、the included Views (widgets) and their properties. A layout can be defined via Java code or via XML. In most cases the layout is defined as an XML file. XML based layouts are defined via a resource file in the /res/layout folder. This file specifies the ViewGroups, Views, their relationship and their attributes for