Android应用架构外文翻译.doc

上传人:文库蛋蛋多 文档编号:2393594 上传时间:2023-02-17 格式:DOC 页数:10 大小:50.50KB
返回 下载 相关 举报
Android应用架构外文翻译.doc_第1页
第1页 / 共10页
Android应用架构外文翻译.doc_第2页
第2页 / 共10页
Android应用架构外文翻译.doc_第3页
第3页 / 共10页
Android应用架构外文翻译.doc_第4页
第4页 / 共10页
Android应用架构外文翻译.doc_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《Android应用架构外文翻译.doc》由会员分享,可在线阅读,更多相关《Android应用架构外文翻译.doc(10页珍藏版)》请在三一办公上搜索。

1、Android Application Architectureauthor:Lars Vogel1、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 perm

2、issions 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 qualified pa

3、ckage 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 yo

4、ur 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

5、, 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 (action

6、 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 th

7、e 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

8、 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 must be

9、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 static int

10、 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 Eclipse de

11、velopment 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 AssetsManager w

12、hich you can access the getAssets() method. AssetsManager allows to read an assets as InputStream with the open() method./ Get the AssetManagerAssetManager manager = getAssets();/ Read a Bitmap from Assetstry InputStream open = manager.open(logo.png);Bitmap bitmap = BitmapFactory.decodeStream(open);

13、/ Assign the bitmap to an ImageView in this layoutImageView 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 the included Views

14、 (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 t

15、his specific layout. If a View needs to be accessed via Java code, you have to give the View a unique ID via the android:id attribute. To assign a new ID to a View use +id/yourvalue. The following shows an example in which a Button gets the button1 ID assigned. By conversion this will create and ass

16、ign a new yourvalue ID to the corresponding View. In your Java code you can later access a View via the method findViewById(R.id.yourvalue). Defining layouts via XML is usually the preferred way as this separates the programming logic from the layout definition. It also allows the definition of diff

17、erent layouts for different devices. You can also mix both approaches. 5、Reference to resources in XML files In your XML files, for example your layout files, you can refer to other resources via the sign. For example, if you want to refer to a color which is defined in a XML resource, you can refer

18、 to it via color/your_id. Or if you defined a hello string in an XML resource, you could access it via string/hello. 6、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 incom

19、ing call. The Android system defines a lifecycle for Activities via predefined methods. The most important methods are: onSaveInstanceState() - called if the Activity is stopped. Used to save data so that the Activity can restore its states if re-started onPause() - always called if the Activity end

20、s, can be used to release resource or save data onResume() - called if the Activity is re-started, can be used to initialize fields 7、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 ma

21、y 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 o

22、rientation via CNTR+F11. You can avoid a restart of your application for certain configuration changes via the configChanges attribute on your Activity definition in your AndroidManifest.xml. The following Activity will not be restarted in case of orientation changes or position of the physical keyb

23、oard (hidden / visible). 8、Context The class android.content.Context provides the connections to the Android system. It is the interface to global information about the application environment. Context also provides access to Android Services, e.g. the Location Service. Activities and Services exten

24、d the Context class and can therefore be used as Context.Android应用架构作者:Lars Vogel(拉尔斯沃格尔)1、AndroidManifest.xml 一个Android应用程序的组件和设置描述文件中的AndroidManifest.xml。例如,应用程序的所有活动和服务,必须在这个文件中声明。它也必须包含应用程序所需的权限。例如,如果应用程序需要访问网络,它必须在这里指定。包属性定义在这个文件中提到的Java 对象的基本包。如果一个Java对象位于不同的包内,这必须声明的完整包名。Google Play 要求,每一个And

25、roid应用程序使用其唯一的包。因此,使用反向域名作为包名是推荐方式。这将避免与其他Android应用程序的冲突。Android:versionName和Android:versionCode指定的应用程序版本。 versionName是用户看到的,可以是任何字符串。versionCode必须为整数。 Android Market的确定根据上versionCode,是否应该对现有装置的应用进行更新。你通常用“1”和增加这个值,如果转出您的应用程序的新版本。标签定义活动,在此指向在de.vogella.android.temperature包Convert类的例子。意图过滤器注册这个类的定义,这

26、个活动开始(action android:name=android.intent.action.MAIN)自从应用程序开始时。这个类定义类别android:name=android.intent.category.LAUNCHER 定义该应用程序被添加到Android设备上的应用程序目录。string/ APP_NAME的值是指资源文件,其中包含应用程序的名称的实际价值。使用资源文件,可以很容易地提供不同的资源,如字符串,颜色,图标,为不同的设备,可以很容易地转化应用。“AndroidManifest.xml”文件的“uses-SDK”部分定义了最小的SDK版本,为您的应用程序是有效的。这将防

27、止您的应用程序被安装在与旧的SDK版本的设备上。2、R.java文件和资源 在一个Android项目中,“gen”目录包含生成的值。 R.java文件是一个自动生成的类,其中包含引用某些资源的项目。 这些资源必须定义在“res”目录中,可以是XML文件,图标或图片。例如,通过XML文件,您可以定义值,菜单布局或动画。如果你创建一个新的资源,相应的参考会自动创建在R.java文件通过Eclipse ADT的工具。这些引用是静态的int值和定义ID的资源。 Android系统提供的方法通过这些ID来访问相应的资源。 例如访问一个String与R.string.yourString ID,你会使用的

28、getString(R.string.yourString)的方法。R.java文件是Eclipse开发环境自动创建的,手动更改是没有必要并且将通过工具重写。3、Assets 众所周知Android平台“res”目录包含结构化的值,这些资源目录可以用来存储任何类型的数据。待添加的隐藏文字内容3您可以访问这些数据通过AssetsManager类的getAssets()方法访问。AssetsManager类允许读取资源输出流,通过open()方法。/ 得到 AssetManagerAssetManager manager = getAssets();/ 从资源中读取一张位图try InputStr

29、eam open = manager.open(logo.png);Bitmap bitmap = BitmapFactory.decodeStream(open);/ 指定位图到ImageViewImageView view = (ImageView) findViewById(R.id.imageView1);view.setImageBitmap(bitmap); catch (IOException e) e.printStackTrace();4、活动和布局 用户接口是通过布局定义的活动,布局定义所包含的意见(部件)及其属性。 一个布局可以通过java代码或是通过XML定义,在大多数

30、案例中,布局被定义为一个XML文件。 基于XML布局被定义通过一个资源文件在/ RES/ layout文件夹中,这个文件专门为了这个特别的布局的ViewsGroups、Views、他们的关系和属性。 如果一个视图需要通过Java代码来访问,你必须通过Android:id属性得到视图的唯一ID 。分配一个新的ID来查看使用+ ID/ 值,下面显示了一个例子,其中一个按钮得到“button1”的ID分配。通过转换,这将创造和分配到相应的视图的新你的ID值。在Java代码中,你可以在以后通过查看方法findViewById(R.id.你的ID值)来访问。通常是通过XML定义布局的首选方式,因为这分开

31、的布局定义的编程逻辑。它也可以定义为不同的设备使用不同的布局。你也可以混合使用这两种方法。 5、引用XML文件中的资源在你的XML文件,例如你的布局文件,你可以参考其他资源,通过符号。例如,如果你想引用到这是在XML资源定义的颜色,你可以参考它通过color/ your_id。或者如果您在XML资源定义一个“hello”字符串,你可以访问这通过string/hello。6、活动和生命周期Android系统的控制您的应用程序的生命周期。在任何时候,Android系统可能会停止或破坏您的应用程序,例如因为一个来电。 Android系统的定义为通过预定义的方法,活动的生命周期。最重要的方法是:onS

32、aveInstanceState() - 如果停止活动。用于保存数据,如果重新启动该活动可以恢复其状态 onPause()- 如果活动结束该方法总是被调用,可以用来释放资源或保存数据onResume()- 如果活动被重新启动,该方法被调用,可以用来初始化字段7、配置变化一个活动也将被重新启动,如果一个所谓的“配置变化”发生。如果事件被触发,这可能是应用程序的相关配置发生变化。例如,如果用户改变设备的方向(纵向或横向)。 Android的假定活动可能要使用不同的资源,这些方向和重新启动的活动。你可以在模拟器模拟通过CNTR+ F11键的方向转变。你能避免某些配置更改,通过configChanges活动的定义在你的AndroidManifest.xml属性的重新启动您的应用程序。下列活动将在取向的变化或物理键盘的位置(隐藏/可见)的情况下无法重新启动。8、上下文(Context)android.content.Context类提供Android系统的连接。它是全球信息化的应用环境有关的接口。Context也提供了访问Android服务,例如位置服务。活动和服务扩展了Context类,因此,可以作为Context使用。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号