Android Application Fundamentals——Android應用程式基底礎知識

來源:互聯網
上載者:User

Android Application Fundamentals——Android應用程式基底礎知識
 Application Fundamentals——應用程式基底礎知識

Key classes——關鍵類

  1. Activity
  2. Service
  3. BroadcastReceiver
  4. ContentProvider
  5. IntentIn this document——在這篇文章中
    1. Application Components——應用程式組件
      1. Activating components: intents——啟用組件:意圖
      2. Shutting down components——關閉組件
      3. The manifest file——資訊清單檔
      4. Intent filters——意圖匹配器
      5. Activities and Tasks——活動和任務
        1. Affinities and new tasks——親和力和新任務
        2. Launch modes——載入樣式
        3. Clearing the stack——清除棧
        4. Starting tasks——開始任務
        5. Processes and Threads——進程和線程
          1. Processes——進程
          2. Threads——線程
          3. Remote procedure calls——遠端程序呼叫
          4. Thread-safe methods——線程保險技術的方法
          5. Component Lifecycles——組件生命週期
            1. Activity lifecycle——活動生命週期
            2. Service lifecycle——服務生命週期
            3. Broadcast receiver lifecycle——廣播接收器生命週期
            4. Processes and lifecycles——進程和生命週期

              Android applications are written in the Java programming language. The compiled Java code — along with any data and resource files required by the application — is bundled by the aapt tool into an Android package, an archive file marked by an .apk suffix. This file is the vehicle for distributing the application and installing it on mobile devices; it's the file users download to their devices. All the code in a single .apk file is considered to be one application.
              Android應用程式是用Java程式設計語言編寫的。編譯好的Java代碼——隨同所有應用程式要使用的資料和資源檔一起——使用aapt tool打包成一個Android包,即一個尾碼為.apk的歸檔檔案。此檔案是發布應用程式和在行動裝置上安裝應用程式的有媒介;它是使用者下載到他們裝置上的檔案。在一個.apk檔案中的所有代碼被認為是一個應用程式。

              In many ways, each Android application lives in its own world:
              從很多方面來講,每一個Android程式都運行在自己的空間裡:

              • By default, every application runs in its own Linux process. Android starts the process when any of the application's code needs to be executed, and shuts down the process when it's no longer needed and system resources are required by other applications. 預設的,每一個程式運行在其自己的Linux進程中。Android在應用程式中某段代碼需要被執行時啟動一個進程,不再使用或系統資源被其他應用程式請求時關閉這個進程。
              • Each process has its own Java virtual machine (VM), so application code runs in isolation from the code of all other applications. 每一個進程都有自己的Java虛擬機器(VM),一個應用程式代碼運行時同其他程式碼分開,不受任何其他程式的代碼的影響。
              • By default, each application is assigned a unique Linux user ID. Permissions are set so that the application's files are visible only that user, only to the application itself — although there are ways to export them to other applications as well. 預設的,每個應用程式享有一個獨自的Linux使用者ID。由於許可權設定原因,一個應用程式的檔案只有本使用者(其自己)可見——當然,也有把他們匯出給其他應用程式的機制。

                It's possible to arrange for two applications to share the same user ID, in which case they will be able to see each other's files. To conserve system resources, applications with the same ID can also arrange to run in the same Linux process, sharing the same VM.
                可以為兩個應用程式安排為使用同一個使用者ID,這樣,他們的檔案相互可見。為為了節省系統資源,擁有相同ID的程式可以作為一個Linux進程運行,分享同一個虛擬機器。

                 

                Application Components——應用程式組件

                A central feature of Android is that one application can make use of elements of other applications (provided those applications permit it). For example, if your application needs to display a scrolling list of images and another application has developed a suitable scroller and made it available to others, you can call upon that scroller to do the work, rather than develop your own. Your application doesn't incorporate the code of the other application or link to it. Rather, it simply starts up that piece of the other application when the need arises.
                Android的一個中心功能就是應用程式可以使用其他應用程式的元素(在提供元素的應用程式允許的情況下)。例如,如果您的應用程式想要顯示一系列帶有滑屏效果功能的圖片,然後某個應用程式正巧開發出了合適的滑屏的模組,並且同意共用,您就可以調用哪個滑屏模組處理這些圖片並顯示出來,而不是您自己再去開發一個。您的應用程式並沒有包含或連結到了其他的應用程式的代碼。但是當請求發出後,您的應用程式確實可以簡單的使用其他程式的部分功能。

                For this to work, the system must be able to start an application process when any part of it is needed, and instantiate the Java objects for that part. Therefore, unlike applications on most other systems, Android applications don't have a single entry point for everything in the application (no main() function, for example). Rather, they have essentialcomponents that the system can instantiate and run as needed. There are four types of components:
                為了達成這個過程,系統必須可以再應用程式的某個部分被請求時啟動這個程式的進程。然後為那個部分的Java對象建立執行個體。因此不像其他動作系統上的應用程式那樣,Android程式沒有一個單一的應用程式入口(例如沒有main()函數)。然而他們含有當系統需要時建立執行個體的實質的組件。組件有四種:

                Activities——活動
                An activity presents a visual user interface for one focused endeavor the user can undertake. For example, an activity might present a list of menu items users can choose from or it might display photographs along with their captions. A text messaging application might have one activity that shows a list of contacts to send messages to, a second activity to write the message to the chosen contact, and other activities to review old messages or change settings. Though they work together to form a cohesive user interface, each activity is independent of the others. Each one is implemented as a subclass of the Activity base class.

                Whenever there's a request that should be handled by a particular component, Android makes sure that the application process of the component is running, starting it if necessary, and that an appropriate instance of the component is available, creating the instance if necessary.
                當有應被特殊的組件操作的請求時,Android會確認這個組件是否正在運行,如果未在運行,則啟動它,如果正在運行則檢測需要的類是否執行個體化,如未執行個體化,則為類建立執行個體。

                Activating components: intents——啟用別的組件的組件:意圖

                Content providers are activated when they're targeted by a request from a ContentResolver. The other three components — activities, services, and broadcast receivers — are activated by asynchronous messages called intents. An intent is an Intent object that holds the content of the message. For activities and services, it names the action being requested and specifies the URI of the data to act on, among other things. For example, it might convey a request for an activity to present an image to the user or let the user edit some text. For broadcast receivers, the Intent object names the action being announced. For example, it might announce to interested parties that the camera button has been pressed.
                內容提供者獲得ContentResolver請求的指向後,會被啟用。另外的三種組件——活動,服務,廣播接收器——是靠異一種叫做intents(意圖)的非同步訊息啟用的。意圖是 承載了這種非同步訊息內容的 Intent的對象。對於活動和服務,它主要是為被請求的動作命名,然後指定操作此資料的URI。例如,它可能攜帶讓一個活動為使用者展現一張圖片或者讓使用者編輯文本的請求。對廣播接收器,Intent對象為將要廣播的內容命名。例如,它可能會向有興趣的接收器發布照相鍵被按下的廣播。

                There are separate methods for activiating each type of component:
                啟用不同的組件需使用不同的方法:

                • An activity is launched (or given something new to do) by passing an Intent object to Context.startActivity() or Activity.startActivityForResult(). The responding activity can look at the initial intent that caused it to be launched by calling its getIntent() method. Android calls the activity's onNewIntent() method to pass it any subsequent intents. 向Context.startActivity()或Activity.startActivityForResult()傳遞Intent對象可以啟用(或獲得新的任務)一個活動。響應的活動可以通過調用getIntent()方法查看導致自己被啟用的最初的意圖。Android調用活動的onNewIntent()方法來傳遞之後的意圖。

                  One activity often starts the next one. If it expects a result back from the activity it's starting, it calls startActivityForResult() instead of startActivity(). For example, if it starts an activity that lets the user pick a photo, it might expect to be returned the chosen photo. The result is returned in an Intent object that's passed to the calling activity's onActivityResult() method. 一個活動通常會開啟另一個活動。如果前一個活動希望從新啟動的活動獲得一個運行結果,可以調用startActivityForResult()方法來代替startActivity()方法。例如,如果一個老活動開啟了一個讓使用者照相的新活動,又希望從新活動獲得這張照片。新活動的運行結果是通過一個 傳遞給老活動的onActivityResult()方法的 Intent對象傳遞的。

                • A service is started (or new instructions are given to an ongoing service) by passing an Intent object to Context.startService(). Android calls the service's onStart() method and passes it the Intent object.

                  Similarly, an intent can be passed to Context.bindService() to establish an ongoing connection between the calling component and a target service. The service receives the Intent object in an onBind() call. (If the service is not already running, bindService() can optionally start it.) For example, an activity might establish a connection with the music playback service mentioned earlier so that it can provide the user with the means (a user interface) for controlling the playback. The activity would call bindService() to set up that connection, and then call methods defined by the service to affect the playback. 服務通過向Context.startService()傳遞一個Intent對象啟動(或給予新任務)Android調用服務的onStart()方法然後將Intent對象傳遞給服務。類似的,一個意圖可以被傳給Context.bindService()來建立呼叫組件和被叫組件的即時串連。調用服務的onBind()來使其接受Intent對象。(如果服務未在運行,您可以選擇使用bindService()來啟動它。)例如上面提到過的音樂播放服務,活動會希望建立一個與此服務的串連以向使用者提供控制播放的功能(例如一個UI介面)。這時活動就會調用bindService()來建立串連,然後調用服務中定義的方法來控制播放。

                • A later section, Remote procedure calls, has more details about binding to a service. 之後的章節會講到Remote procedure calls,那有更詳細的綁定服務的資訊。

                • An application can initiate a broadcast by passing an Intent object to methods like Context.sendBroadcast(), Context.sendOrderedBroadcast(), and Context.sendStickyBroadcast() in any of their variations. Android delivers the intent to all interested broadcast receivers by calling their onReceive() methods. 程式可以通過向類似 Context.sendBroadcast(),Context.sendOrderedBroadcast(),Context.sendStickyBroadcast(),及它們的變種 的方法傳遞一個Intent對象來初始化一條廣播。

                  For more on intent messages, see the separate article, Intents and Intent Filters.
                  獲得

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.