Android 手機開發平台


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Android 手機開發平台

  1. 1. Android 手機開發平台
  2. 2. Android 的來源 <ul><li>Google 併購 Android 公司後所推出的手機平台 </li></ul><ul><li>採用 Linux 為作業系統, </li></ul><ul><li>系統程式包含了 C 語言的標準函式庫, </li></ul><ul><li>應用程式主要以 Java 語言在 Dalvik VM 虛擬機器 </li></ul><ul><li>應用程式開發環境以 Eclipse 為主,加上 Android Development Tools (ADT) Plugin. </li></ul>
  3. 3. Android 的架構
  4. 4. 安裝
  5. 5. 第一個程式 - Hello
  6. 6. 應用程式的主要架構 <ul><li>AndroidManifest.xml </li></ul><ul><li>Activity ( 活動 ) </li></ul><ul><li>Intent Receiver </li></ul><ul><li>Service </li></ul><ul><li>Content Provider </li></ul>
  7. 7. AndroidManifest.xml <ul><li>The AndroidManifest.xml file is the control file that tells the system what to do with all the top-level components (specifically activities, services, intent receivers, and content providers described below) you've created. For instance, this is the &quot;glue&quot; that actually specifies which Intents your Activities receive. </li></ul>
  8. 8. AndroidManifest.xml <ul><li><manifest> The root node of the file, describing the complete contents of the package. Under it you can place: </li></ul><ul><ul><li><uses-permission> Requests a security permission that your package must be granted in order for it to operate correctly. See the Security Model document for more information on permissions. A manifest can contain zero or more of these elements. <permission> Declares a security permission that can be used to restrict which applications can access components or features in your (or another) package. See the Security Model document for more information on permissions. A manifest can contain zero or more of these elements. </li></ul></ul><ul><ul><li><instrumentation> Declares the code of an instrumentation component that is available to test the functionality of this or another package. See Instrumentation for more details. A manifest can contain zero or more of these elements. </li></ul></ul><ul><ul><li><application> Root element containing declarations of the application-level components contained in the package. This element can also include global and/or default attributes for the application, such as a label, icon, theme, required permission, etc. A manifest can contain zero or one of these elements (more than one application tag is not allowed). Under it you can place zero or more of each of the following component declarations: </li></ul></ul><ul><ul><ul><li><activity> An Activity is the primary facility for an application to interact with the user. The initial screen the user sees when launching an application is an activity, and most other screens they use will be implemented as separate activities declared with additional activity tags. Note: Every Activity must have an <activity> tag in the manifest whether it is exposed to the world or intended for use only within its own package. If an Activity has no matching tag in the manifest, you won't be able to launch it. </li></ul></ul></ul><ul><ul><ul><li>Optionally, to support late runtime lookup of your activity, you can include one or more <intent-filter> elements to describe the actions the activity supports: </li></ul></ul></ul><ul><ul><ul><ul><li><intent-filter> Declares a specific set of Intent values that a component supports, in the form of an IntentFilter . In addition to the various kinds of values that can be specified under this element, attributes can be given here to supply a unique label, icon, and other information for the action being described. </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li><action> An Intent action that the component supports. </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li><category> An Intent category that the component supports. </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li><type> An Intent data MIME type that the component supports. </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li><scheme> An Intent data URI scheme that the component supports. </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li><authority> An Intent data URI authority that the component supports. </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li><path> An Intent data URI path that the component supports. </li></ul></ul></ul></ul></ul><ul><ul><ul><li><receiver> An IntentReceiver allows an application to be told about changes to data or actions that happen, even if it is not currently running. As with the activity tag, you can optionally include one or more <intent-filter> elements that the receiver supports; see the activity's <intent-filter> description for more information. </li></ul></ul></ul><ul><ul><ul><li><service> A Service is a component that can run in the background for an arbitrary amount of time. As with the activity tag, you can optionally include one or more <intent-filter> elements that the receiver supports; see the activity's <intent-filter> description for more information. </li></ul></ul></ul><ul><ul><ul><li><provider> A ContentProvider is a component that manages persistent data and publishes it for access by other applications. </li></ul></ul></ul>
  9. 9. Activity <ul><li>Activity 活動 ( 通常指一個畫面 single screen) </li></ul><ul><ul><li>Each activity is implemented as a single class that extends the Activity base class </li></ul></ul><ul><ul><li>Your class will display a user interface composed of Views and respond to events. </li></ul></ul><ul><ul><li>For example, a text messaging application might have one screen that shows a list of contacts to send messages to, a second screen to write the message to the chosen contact, and other screens to review old messages or change settings. Each of these screens would be implemented as an activity </li></ul></ul><ul><li>Action and Intent </li></ul><ul><ul><li>Android uses a special class called an Intent to move from screen to screen </li></ul></ul><ul><ul><li>The two most important parts of the intent data structure are the action and the data to act upon. </li></ul></ul><ul><ul><li>Typical values for action are MAIN (the front door of the activity), VIEW, PICK, EDIT </li></ul></ul><ul><ul><li>For example, to view contact information for a person, you would create an intent with the VIEW action and the data set to a URI representing that person. </li></ul></ul>
  10. 10. View <ul><li>A View is an object that knows how to draw itself to the screen. Android user interfaces are comprised of trees of Views. If you want to perform some custom graphical technique (as you might if you're writing a game, or building some unusual new user interface widget) then you'd create a View. </li></ul>
  11. 11. Intent <ul><li>An Intent is a simple message object that represents an &quot;intention&quot; to do something. For example, if your application wants to display a web page, it expresses its &quot;Intent&quot; to view the URI by creating an Intent instance and handing it off to the system. The system locates some other piece of code (in this case, the Browser) that knows how to handle that Intent, and runs it. Intents can also be used to broadcast interesting events (such as a notification) system-wide. </li></ul>
  12. 12. Intent Receiver <ul><li>You can use an IntentReceiver when you want code in your application to execute in reaction to an external event, </li></ul><ul><li>for example, when the phone rings, or when the data network is available, or when it's midnight. Intent receivers do not display a UI, although they may use the NotificationManager to alert the user if something interesting has happened. Intent receivers are registered in AndroidManifest.xml, but you can also register them from code using Context.registerReceiver() . </li></ul><ul><li>Your application does not have to be running for its intent receivers to be called; the system will start your application, if necessary, when an intent receiver is triggered. Applications can also send their own intent broadcasts to others with Context.broadcastIntent() . </li></ul>
  13. 13. Service <ul><li>A Service is a body of code that runs in the background. It can run in its own process, or in the context of another application's process, depending on its needs. Other components &quot;bind&quot; to a Service and invoke methods on it via remote procedure calls. An example of a Service is a media player; even when the user quits the media-selection UI, she probably still intends for her music to keep playing. A Service keeps the music going even when the UI has completed. </li></ul><ul><li>A Service is code that is long-lived and runs without a UI. </li></ul><ul><li>A good example of this is a media player playing songs from a play list. In a media player application, there would probably be one or more activities that allow the user to choose songs and start playing them. However, the music playback itself should not be handled by an activity because the user will expect the music to keep playing even after navigating to a new screen. </li></ul><ul><li>In this case, the media player activity could start a service using Context.startService() to run in the background to keep the music going. The system will then keep the music playback service running until it has finished. (You can learn more about the priority given to services in the system by reading Lifecycle of an Android Application .) </li></ul><ul><li>Note that you can connect to a service (and start it if it's not already running) with the Context.bindService() method. When connected to a service, you can communicate with it through an interface exposed by the service. For the music service, this might allow you to pause, rewind, etc. </li></ul>
  14. 14. Notification <ul><li>A Notification is a small icon that appears in the status bar. Users can interact with this icon to receive information. The most well-known notifications are SMS messages, call history, and voicemail, but applications can create their own. Notifications are the strongly-preferred mechanism for alerting the user of something that needs their attention </li></ul>
  15. 15. Content Provider <ul><li>A ContentProvider is a data storehouse that provides access to data on the device; the classic example is the ContentProvider that's used to access the user's list of contacts. Your application can access data that other applications have exposed via a ContentProvider, and you can also define your own ContentProviders to expose data of your own. </li></ul><ul><li>Applications can store their data in files, an SQLite database, or any other mechanism that makes sense. </li></ul><ul><li>A content provider, however, is useful if you want your application's data to be shared with other applications. </li></ul><ul><li>A content provider is a class that implements a standard set of methods to let other applications store and retrieve the type of data that is handled by that content provider </li></ul>
  16. 16. Lifecycle of an Android Application <ul><li>In most cases, every Android application runs in its own Linux process. </li></ul><ul><li>This process is created for the application when some of its code needs to be run, and will remain running until it is no longer needed and the system needs to reclaim its memory for use by other applications. </li></ul><ul><li>It is important that application developers understand how different application components (in particular Activity , Service , and IntentReceiver ) impact the lifetime of the application's process. Not using these components correctly can result in the system killing the application's process while it is doing important work. </li></ul><ul><li>A common example of a process lifecycle bug is an IntentReceiver that starts a thread when it receives an Intent in its onReceiveIntent() method, and then returns from the function. Once it returns, the system considers that IntentReceiver to be no longer active, and thus its hosting process no longer needed (unless other application components are active in it). Thus, it may kill the process at any time to reclaim memory, terminating the spawned thread that is running in it. </li></ul><ul><li>The solution to this problem is to start a Service from the IntentReceiver, so the system knows that there is still active work being done in the process. </li></ul>
  17. 17. UI <ul><li>View </li></ul><ul><li>Layout </li></ul>
  18. 18. View
  19. 19. Layout
  20. 20. Layout <ul><li>FrameLayout </li></ul><ul><li>LinearLayout </li></ul><ul><li>TableLayout </li></ul><ul><li>AbsoluteLayout </li></ul><ul><li>RelativeLayout </li></ul>
  21. 21. LinearLayout
  22. 22. TableLayout
  23. 23. RelativeLayout
  24. 24. Hierarchy of Screen Elements <ul><li>Views </li></ul><ul><li>Viewgroups </li></ul><ul><li>A Tree-Structured UI </li></ul><ul><li>LayoutParams: </li></ul><ul><ul><li>How a Child Specifies Its Position and Size </li></ul></ul>
  25. 25. Storing, Retrieving and Exposing Data
  26. 26. Preferences <ul><li>Preferences A lightweight mechanism to store and retrieve key/value pairs of primitive data types. This is typically used to store application preferences. </li></ul>
  27. 27. Files <ul><li>Files You can store your files on the device or on a removable storage medium. By default, other applications cannot access these files. </li></ul>
  28. 28. Databases <ul><li>Databases The Android APIs contain support for SQLite. Your application can create and use a private SQLite database. Each database is private to the package that creates it. </li></ul>
  29. 29. Content Providers <ul><li>Content Providers A content provider is a optional component of an application that exposes read/write access to an application's private data, subject to whatever restrictions it wants to impose. Content providers implement a standard request syntax for data, and a standard access mechanism for the returned data. Android supplies a number of content providers for standard data types, such as personal contacts. </li></ul>
  30. 30. Network <ul><li>Network Don't forget that you can also use the network to store and retrieve data. </li></ul>
  31. 31. Asynchronous RPC <ul><li>The combination of startSubActivity() and onActivityResult() can be thought of as an asynchronous RPC (remote procedure call) and forms the recommended way for Activities to invoke each other and share services </li></ul>
  32. 32. Bundle <ul><li>Bundle 是一個像 Map 一樣的東西, Android 用它來傳遞訊息 ( 物件結構 ). </li></ul>