android應(yīng)用架構(gòu)外文翻譯_第1頁(yè)
已閱讀1頁(yè),還剩9頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、<p>  Android Application Architecture</p><p>  author:Lars Vogel</p><p>  1、AndroidManifest.xml </p><p>  The components and settings of an Android application are described i

2、n the file AndroidManifest.xml. For example all Activities and Services of the application must be declared in this file. </p><p>  It must also contain the required permissions for the application. For exam

3、ple if the application requires network access it must be specified here.</p><p>  <?xml version="1.0" encoding="utf-8"?></p><p>  <manifest xmlns:android="http

4、://schemas.android.com/apk/res/android"</p><p>  package="de.vogella.android.temperature"</p><p>  android:versionCode="1"</p><p>  android:versionName=&quo

5、t;1.0"></p><p>  <application android:icon="@drawable/icon" android:label="@string/app_name"></p><p>  <activity android:name=".Convert"</p>

6、<p>  android:label="@string/app_name"></p><p>  <intent-filter></p><p>  <action android:name="android.intent.action.MAIN" /></p><p>  <

7、category android:name="android.intent.category.LAUNCHER" /></p><p>  </intent-filter></p><p>  </activity></p><p>  </application></p><p>

8、;  <uses-sdk android:minSdkVersion="9" /></p><p>  </manifest></p><p>  The package attribute defines the base package for the Java objects referred to in this file. If a J

9、ava object lies within a different package, it must be declared with the full qualified package name. </p><p>  Google Play requires that every Android application uses its own unique package. Therefore it i

10、s a good habit to use your reverse domain name as package name. This will avoid collisions with other Android applications. </p><p>  android:versionName and android:versionCode specify the version of your a

11、pplication. versionName is what the user sees and can be any String. </p><p>  versionCode must be an integer. The Android Market determine based on the versionCode, if it should perform an update of the app

12、lications 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. </p><p>  The tag <activity> defines

13、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 sta

14、rts (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 dire

15、ctory on the Android device. </p><p>  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 differen

16、t resources, e.g. strings, colors, icons, for different devices and makes it easy to translate applications.</p><p>  The "uses-sdk" part of the "AndroidManifest.xml" file defines the min

17、imal SDK version for which your application is valid. This will prevent your application being installed on devices with older SDK versions. </p><p>  2、R.java and Resources </p><p>  The "

18、 gen " directory in an Android project contains generated values. R.java is a generated class which contains references to certain resources of the project. </p><p>  These resources must be defined in

19、the "res" directory and can be XML files, icons or pictures. You can for example define values, menus, layouts or animations via XML files. </p><p>  If you create a new resource, the corresponding

20、 reference is automatically created in R.java via the Eclipse ADT tools. These references are static int values and define ID's for the resources. </p><p>  The Android system provides methods to access

21、the corresponding resource via these ID's. </p><p>  For example to access a String with the R.string.yourString ID, you would use the getString(R.string.yourString)) method. </p><p>  R.jav

22、a is automatically created by the Eclipse development environment, manual changes are not necessary and will be overridden by the tooling. </p><p><b>  3、Assets </b></p><p>  While t

23、he 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 which you can access the getAsse

24、ts() method. </p><p>  AssetsManager allows to read an assets as InputStream with the open() method.</p><p>  // Get the AssetManager</p><p>  AssetManager manager = getAssets();<

25、;/p><p>  // Read a Bitmap from Assets</p><p><b>  try {</b></p><p>  InputStream open = manager.open("logo.png");</p><p>  Bitmap bitmap = BitmapFac

26、tory.decodeStream(open);</p><p>  // Assign the bitmap to an ImageView in this layout</p><p>  ImageView view = (ImageView) findViewById(R.id.imageView1);</p><p>  view.setImageBitm

27、ap(bitmap);</p><p>  } catch (IOException e) {</p><p>  e.printStackTrace();</p><p><b>  }</b></p><p>  4、Activities and Layouts </p><p>  The

28、user interface for Activities is defined via layouts. The layout defines the included Views (widgets) and their properties. </p><p>  A layout can be defined via Java code or via XML. In most cases the layo

29、ut is defined as an XML file. </p><p>  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 th

30、is specific layout. </p><p>  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

31、shows an example in which a Button gets the "button1" ID assigned. </p><p><b>  <Button</b></p><p>  android:id="@+id/button1"</p><p>  android:la

32、yout_width="wrap_content"</p><p>  android:layout_height="wrap_content"</p><p>  android:text="Show Preferences" ></p><p><b>  </Button>

33、</b></p><p>  By conversion this will create and assign 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). </p&g

34、t;<p>  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 different layouts for different devices. You can a

35、lso mix both approaches. </p><p>  5、Reference to resources in XML files </p><p>  In your XML files, for example your layout files, you can refer to other resources via the @ sign. </p>

36、<p>  For example, if you want to refer to a color which is defined in a XML resource, you can refer to it via @color/your_id. Or if you defined a "hello" string in an XML resource, you could access it via

37、 @string/hello. </p><p>  6、Activities and Lifecycle </p><p>  The Android system controls the lifecycle of your application. At any time the Android system may stop or destroy your application,

38、 e.g. because of an incoming call. The Android system defines a lifecycle for Activities via predefined methods. The most important methods are: </p><p>  onSaveInstanceState() - called if the Activity is st

39、opped. Used to save data so that the Activity can restore its states if re-started </p><p>  onPause() - always called if the Activity ends, can be used to release resource or save data </p><p>

40、  onResume() - called if the Activity is re-started, can be used to initialize fields </p><p>  7、Configuration Change </p><p>  An Activity will also be restarted, if a so called "configur

41、ation 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). A

42、ndroid assumes that an Activity might want to use different resources for these orientations and restarts the Activity. </p><p>  In the emulator you can simulate the change of the orientation via CNTR+F11.

43、</p><p>  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 no

44、t be restarted in case of orientation changes or position of the physical keyboard (hidden / visible). </p><p>  <activity android:name=".ProgressTestActivity"</p><p>  android:labe

45、l="@string/app_name"</p><p>  android:configChanges="orientation|keyboardHidden|keyboard"></p><p>  </activity></p><p>  8、Context </p><p>

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

47、Location Service. Activities and Services extend the Context class and can therefore be used as Context.</p><p>  Android應(yīng)用架構(gòu)</p><p>  作者:Lars Vogel(拉爾斯·沃格爾)</p><p>  1、Android

48、Manifest.xml </p><p>  一個(gè)Android應(yīng)用程序的組件和設(shè)置描述文件中的AndroidManifest.xml。例如,應(yīng)用程序的所有活動(dòng)和服務(wù),必須在這個(gè)文件中聲明。它也必須包含應(yīng)用程序所需的權(quán)限。例如,如果應(yīng)用程序需要訪問(wèn)網(wǎng)絡(luò),它必須在這里指定。</p><p>  <?xml version="1.0" encoding="

49、utf-8"?></p><p>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"</p><p>  package="de.vogella.android.temperature"</p><p>  andr

50、oid:versionCode="1"</p><p>  android:versionName="1.0"></p><p>  <application android:icon="@drawable/icon" android:label="@string/app_name"><

51、/p><p>  <activity android:name=".Convert"</p><p>  android:label="@string/app_name"></p><p>  <intent-filter></p><p>  <action android:

52、name="android.intent.action.MAIN" /></p><p>  <category android:name="android.intent.category.LAUNCHER" /></p><p>  </intent-filter></p><p>  <

53、/activity></p><p>  </application></p><p>  <uses-sdk android:minSdkVersion="9" /></p><p>  </manifest></p><p>  包屬性定義在這個(gè)文件中提到的Java 對(duì)象的基本

54、包。如果一個(gè)Java對(duì)象位于不同的包內(nèi),這必須聲明的完整包名。Google Play 要求,每一個(gè)Android應(yīng)用程序使用其唯一的包。因此,使用反向域名作為包名是推薦方式。這將避免與其他Android應(yīng)用程序的沖突。Android:versionName和Android:versionCode指定的應(yīng)用程序版本。 versionName是用戶看到的,可以是任何字符串。versionCode必須為整數(shù)。 Android Market的確

55、定根據(jù)上versionCode,是否應(yīng)該對(duì)現(xiàn)有裝置的應(yīng)用進(jìn)行更新。你通常用“1”和增加這個(gè)值,如果轉(zhuǎn)出您的應(yīng)用程序的新版本。</p><p>  標(biāo)簽<activity>定義活動(dòng),在此指向在de.vogella.android.temperature包Convert類的例子。意圖過(guò)濾器注冊(cè)這個(gè)類的定義,這個(gè)活動(dòng)開(kāi)始(action android:name="android.intent.a

56、ction.MAIN")自從應(yīng)用程序開(kāi)始時(shí)。這個(gè)類定義類別android:name="android.intent.category.LAUNCHER" 定義該應(yīng)用程序被添加到Android設(shè)備上的應(yīng)用程序目錄。</p><p>  @string/ APP_NAME的值是指資源文件,其中包含應(yīng)用程序的名稱的實(shí)際價(jià)值。使用資源文件,可以很容易地提供不同的資源,如字符串,顏色,圖標(biāo),為

57、不同的設(shè)備,可以很容易地轉(zhuǎn)化應(yīng)用。</p><p>  “AndroidManifest.xml”文件的“uses-SDK”部分定義了最小的SDK版本,為您的應(yīng)用程序是有效的。這將防止您的應(yīng)用程序被安裝在與舊的SDK版本的設(shè)備上。</p><p>  2、R.java文件和資源</p><p>  在一個(gè)Android項(xiàng)目中,“gen”目錄包含生成的值。 R.jav

58、a文件是一個(gè)自動(dòng)生成的類,其中包含引用某些資源的項(xiàng)目。</p><p>  這些資源必須定義在“res”目錄中,可以是XML文件,圖標(biāo)或圖片。例如,通過(guò)XML文件,您可以定義值,菜單布局或動(dòng)畫(huà)。</p><p>  如果你創(chuàng)建一個(gè)新的資源,相應(yīng)的參考會(huì)自動(dòng)創(chuàng)建在R.java文件通過(guò)Eclipse ADT的工具。這些引用是靜態(tài)的int值和定義ID的資源。</p><p&g

59、t;  Android系統(tǒng)提供的方法通過(guò)這些ID來(lái)訪問(wèn)相應(yīng)的資源。</p><p>  例如訪問(wèn)一個(gè)String與R.string.yourString ID,你會(huì)使用的getString(R.string.yourString)的方法。</p><p>  R.java文件是Eclipse開(kāi)發(fā)環(huán)境自動(dòng)創(chuàng)建的,手動(dòng)更改是沒(méi)有必要并且將通過(guò)工具重寫(xiě)。</p><p>

60、<b>  3、Assets </b></p><p>  眾所周知Android平臺(tái)“res”目錄包含結(jié)構(gòu)化的值,這些資源目錄可以用來(lái)存儲(chǔ)??任何類型的數(shù)據(jù)。</p><p>  您可以訪問(wèn)這些數(shù)據(jù)通過(guò)AssetsManager類的getAssets()方法訪問(wèn)。</p><p>  AssetsManager類允許讀取資源輸出流,通過(guò)ope

61、n()方法。</p><p>  // 得到 AssetManager</p><p>  AssetManager manager = getAssets();</p><p>  // 從資源中讀取一張位圖</p><p><b>  try {</b></p><p>  InputStre

62、am open = manager.open("logo.png");</p><p>  Bitmap bitmap = BitmapFactory.decodeStream(open);</p><p>  // 指定位圖到ImageView</p><p>  ImageView view = (ImageView) findViewByI

63、d(R.id.imageView1);</p><p>  view.setImageBitmap(bitmap);</p><p>  } catch (IOException e) {</p><p>  e.printStackTrace();</p><p><b>  }</b></p><

64、p><b>  4、活動(dòng)和布局</b></p><p>  用戶接口是通過(guò)布局定義的活動(dòng),布局定義所包含的意見(jiàn)(部件)及其屬性。</p><p>  一個(gè)布局可以通過(guò)java代碼或是通過(guò)XML定義,在大多數(shù)案例中,布局被定義為一個(gè)XML文件。</p><p>  基于XML布局被定義通過(guò)一個(gè)資源文件在/ RES/ layout文件夾中,這

65、個(gè)文件專門為了這個(gè)特別的布局的ViewsGroups、Views、他們的關(guān)系和屬性。</p><p>  如果一個(gè)視圖需要通過(guò)Java代碼來(lái)訪問(wèn),你必須通過(guò)Android:id屬性得到視圖的唯一ID 。分配一個(gè)新的ID來(lái)查看使用@+ ID/ 值,下面顯示了一個(gè)例子,其中一個(gè)按鈕得到“button1”的ID分配。</p><p><b>  <Button</b>

66、</p><p>  android:id="@+id/button1"</p><p>  android:layout_width="wrap_content"</p><p>  android:layout_height="wrap_content"</p><p>  an

67、droid:text="Show Preferences" ></p><p><b>  </Button></b></p><p>  通過(guò)轉(zhuǎn)換,這將創(chuàng)造和分配到相應(yīng)的視圖的新你的ID值。在Java代碼中,你可以在以后通過(guò)查看方法findViewById(R.id.你的ID值)來(lái)訪問(wèn)。</p><p>

68、  通常是通過(guò)XML定義布局的首選方式,因?yàn)檫@分開(kāi)的布局定義的編程邏輯。它也可以定義為不同的設(shè)備使用不同的布局。你也可以混合使用這兩種方法。</p><p>  5、引用XML文件中的資源</p><p>  在你的XML文件,例如你的布局文件,你可以參考其他資源,通過(guò)@符號(hào)。</p><p>  例如,如果你想引用到這是在XML資源定義的顏色,你可以參考它通過(guò)@c

69、olor/ your_id?;蛘呷绻赬ML資源定義一個(gè)“hello”字符串,你可以訪問(wèn)這通過(guò)@string/hello。</p><p><b>  6、活動(dòng)和生命周期</b></p><p>  Android系統(tǒng)的控制您的應(yīng)用程序的生命周期。在任何時(shí)候,Android系統(tǒng)可能會(huì)停止或破壞您的應(yīng)用程序,例如因?yàn)橐粋€(gè)來(lái)電。 Android系統(tǒng)的定義為通過(guò)預(yù)定義的方

70、法,活動(dòng)的生命周期。最重要的方法是:</p><p>  onSaveInstanceState() - 如果停止活動(dòng)。用于保存數(shù)據(jù),如果重新啟動(dòng)該活動(dòng)可以恢復(fù)其狀態(tài) </p><p>  onPause()- 如果活動(dòng)結(jié)束該方法總是被調(diào)用,可以用來(lái)釋放資源或保存數(shù)據(jù)</p><p>  onResume()- 如果活動(dòng)被重新啟動(dòng),該方法被調(diào)用,可以用來(lái)初始化字段&

71、lt;/p><p><b>  7、配置變化</b></p><p>  一個(gè)活動(dòng)也將被重新啟動(dòng),如果一個(gè)所謂的“配置變化”發(fā)生。如果事件被觸發(fā),這可能是應(yīng)用程序的相關(guān)配置發(fā)生變化。例如,如果用戶改變?cè)O(shè)備的方向(縱向或橫向)。 Android的假定活動(dòng)可能要使用不同的資源,這些方向和重新啟動(dòng)的活動(dòng)。</p><p>  你可以在模擬器模擬通過(guò)CNT

72、R+ F11鍵的方向轉(zhuǎn)變。</p><p>  你能避免某些配置更改,通過(guò)configChanges活動(dòng)的定義在你的AndroidManifest.xml屬性的重新啟動(dòng)您的應(yīng)用程序。下列活動(dòng)將在取向的變化或物理鍵盤的位置(隱藏/可見(jiàn))的情況下無(wú)法重新啟動(dòng)。</p><p>  <activity android:name=".ProgressTestActivity&quo

73、t;</p><p>  android:label="@string/app_name" android:configChanges="orientation|keyboardHidden|keyboard"></p><p>  </activity></p><p>  8、上下文(Contex

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 眾賞文庫(kù)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論