1 / 85

Android Development Introduction

Android Development Introduction. Notes are based on: Unlocking Android by Frank Ableson , Charlie Collins, and Robi Sen. ISBN 978-1-933988-67-2 Manning Publications, 2009. & Android Developers http://developer.android.com/index.html. Chapter 1 - Goals. THE BIG PICTURE

fayre
Download Presentation

Android Development Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Android DevelopmentIntroduction Notes are based on: Unlocking Android by Frank Ableson, Charlie Collins, and Robi Sen. ISBN 978-1-933988-67-2 Manning Publications, 2009. & Android Developers http://developer.android.com/index.html

  2. Chapter 1 - Goals THE BIG PICTURE • What is Android? • Overview development environment

  3. Chapter 1 - Resources Android’s web page http://www.android.com/

  4. What is Android? • Android is an open-source software platform created by Google and the Open Handset Alliance. • It is primarily used to power mobile phones. • It has the capability to make inroads in many other (non-phone) embedded application markets.

  5. What is Android? • Android™ consists of a complete set of software components for mobile devices including: • an operating system, • middleware, and • embedded key mobile applications • a large market.

  6. Why Android? Listen from the project creators/developers (2.19 min) • Nick Sears. Co-founder of Android • Steve Horowitz. Engineering Director • Dam Morrill. Developer • Peisun Wu. Engineering Project Manager • Erick Tseng. Project Manager • IliyanMalchev. Engineer • Mike Cleron. Software Manager • Per Gustafsson. Graphics Designer. • etc… • http://www.youtube.com/watch?v=6rYozIZOgDk&eurl=http://www.android.com/about/&feature=player_embedded • You will hear statements such as “…currently it is too difficult to make new products … open software brings more innovation … choices … lower costs … more applications such as family planner, my taxes, understand my wife better, … ”

  7. What is Open Handset Alliance? • Quoting from www.OpenHandsetAlliance.compage • “… Open Handset Alliance™, a group of 47 technology and mobile companies have come together to accelerate innovation in mobile and offer consumers a richer, less expensive, and better mobile experience. • Together we have developed Android™, the first complete, open, and free mobile platform. • We are committed to commercially deploy handsets and services using the Android Platform. “

  8. Open Handset Alliance Members

  9. See Android Developers http://www.youtube.com/watch?v=7Y4thikv-OM Short video (4 min.) Showing Dave Bort and Dan Borstein, two members of the Android Open Source Project talk about the project.

  10. The Android PlatformAgain, what did they say about Android? • Android is a softwareenvironment built for mobile devices. • It is not a hardware platform. • Android includes: • Linux kernel-based OS, • a rich UI, • telephone functionality, • end-user applications, • code libraries, • application frameworks, • multimedia support, ... • User applications are built for Android in Java.

  11. Android’s Context: Mobile Market Player$ Stakeholders: Mobile networkoperators want to lock down their networks, controlling and metering traffic. Devicemanufacturers want to differentiate themselves with features, reliability, and price points. Softwarevendors want complete access to the hardware to deliver cutting-edge applications.

  12. The Maturing Mobile Experience Electronic tools of a typical business warrior Tomorrow ?

  13. The Maturing Mobile Experience Trying to answer: Tomorrow ?

  14. Android vs. Competitors

  15. The Size of the Mobile Market http://gizmodo.com/5489036/cellphone-overshare [see appendix] 15

  16. Android Components (Stack) • The Android stack includes a large array of features for mobile applications. • It would be easy to confuse Android with a general purpose computing environment. • All of the major components of a computing platform are included.

  17. Android Components • Application framework enabling reuse and replacement of components • Dalvik virtual machine optimized for mobile devices • Integrated browser based on the open source WebKit engine • Optimized graphics powered by a custom 2D graphics library; 3D graphics based on the OpenGL ES specification (hardware acceleration optional) • SQLite for structured data storage • Media support for common audio, video, and still image formats (MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, GIF) • GSM Telephony (hardware dependent) • Bluetooth, EDGE, 3G, 4G, and Wi-Fi (hardware dependent) • Camera, GPS, compass, and accelerometer (hardware dependent) • Rich development environment including a device emulator, tools for debugging, memory and performance profiling, and a plugin for the Eclipse IDE

  18. Android Components

  19. Android Components Video 1/3: Android’s Architecture Presented by Mike Cleron, Google Corp. (13 min) Available at: http://www.youtube.com/watch?v=QBGfUs9mQYY

  20. Android Components Video 2/3: Application’s Life Cycle Presented by Mike Cleron, Google Corp. (8 min) Available at: http://www.youtube.com/watch?v=fL6gSd4ugSI&feature=channel

  21. Android Components Video 3/3: Android’s API Presented by Mike Cleron, Google Corp. (7 min) Available at: http://www.youtube.com/watch?v=MPukbH6D-lY&feature=channel

  22. Android Application Framework Video: Inside the Android Application Framework (about 52 min) Presented by Dan Morrill – Google At Google Developer Conference San Francisco - 2008 Available at: http://sites.google.com/site/io/inside-the-android-application-framework Android is designed to be fast, powerful, and easy to develop for. This session will discuss the Android application framework in depth, showing you the machinery behind the application framework. explains the life-cycle of an android apk. very good!

  23. Android Components Video: An Introduction to Android (about 52 min) Presented by Jason Chen – Google At Google Developer Conference San Francisco - 2008 Available at: http://www.youtube.com/watch?v=x1ZZ-R3p_w8

  24. Why use Linux for a phone? • Linux kernel is a proven core platform. • Reliability is more important than performance when it comes to a mobile phone, because voice communication is the primary use of a phone. • Linux provides a hardware abstraction layer, letting the upper levels remain unchanged despite changes in the underlying hardware. • As new accessories appear on the market, drivers can be written at the Linux level to provide support, just as on other Linux platforms.

  25. Dalvik Virtual Machine • User application, cũng như các ứng dụng core Android, được viết bằng Java và được dịch thành byte code. • Android byte code được thông dịch tại thời gian chạy bởi máy ảo Dalvik (Dalvikvirtual machine). • Tại sao dùng một máy ảo khác? • Các file bytecode Android tương đương về lô-gic với Java bytecode, nhưng chúng cho phép Android • Chạy các ứng dụng trong môi trường ảo của riêng nó, không bị rằng buộc bởi các hạn chế trong giấy phép của Sun • Là một nền tảng mở mà Google và có thể cả cộng đồng mã nguồn mở có thể cải tiến khi cần thiết.

  26. Dalvik Virtual Machine Video (61 min) Dalvik VM Internals Presented by Dan Borstein At Google Developer – 2008 San francisco Available at: http://www.youtube.com/watch?v=ptjedOZEXPM

  27. Inside Android: Intents • Chủ đề quan trọng hay gặp trong phát triển ứng dụng Android là Intent. • Một Intent trong Android mô tả cái ta muốn làm. • Có thể là • “Tôi muốn tra cứu một mục trong contact record,” hoặc • “Hãy mở website này,” hoặc • “Mở màn hình khẳng định đơn đặt hàng.” • Tầm quan trọng của intent : chúng cho phép di chuyển điều khiển trong ứng dụng và là khía cạnh quan trọng nhất trong lập trình Android.

  28. Intents & IntentFilters • An Intent is a declaration of need. • Một Intent bao gồm các mẩu thông tin sau: • action hay service muốn thực hiện, • data – dữ liệu vào cho action/service đó, và • category – loại component nên xử lý intent cùng với các hướng dẫn về cách gọi activity cần gọi. • Một IntentFilter là một trigger, một khai báo về năng lực cung cấp hỗ trợ cho những nơi cần. • Một IntentFilter có thể khai báo tổng quát hoặc cụ thể về các intent mà nó có thể phục vụ.

  29. Intents & IntentFilters • Một intent là một mô tả trừu tượng về một thao tác cần thực hiện. • Ứng dụng quan trọng nhất của nó là cho việc gọi activity (launching of activities), có thể coi intent là keo dán giữa các activity. • Các tham số chính của một intent:

  30. Intents & IntentFilters Ví dụ về các cặp action/data của intent: ACTION_VIEWcontent://contacts/1 – Hiện thông tin về người có id là "1". ACTION_DIALcontent://contacts/1 – Hiện ứng dụng gọi điện thoại cho người có id là “1”. ACTION_VIEWtel:123 -- Hiện ứng dụng gọi điện thoại với số điện thoại điền sẵn là 123 ACTION_DIAL tel:123 – Thực hiện quay số cho số điện thoại 123. ACTION_EDITcontent://contacts/1 – Soạn thông tin về người có id là “1”. ACTION_VIEWcontent://contacts/ -- Hiện danh sách contact, cho phép duyệt danh sách.

  31. Dissecting Intents • Component name Tên component cần xử lý intent ( ví dụ "com.example.project.app.MyActivity1" ). • Action Một string là tên của action cần thực hiện — hoặc, đối với các broadcast intent, action đã xảy ra và đang được báo cáo kết quả (ví dụ: ACTION_VIEW, ACTION_CALL, ACTION_TIMEZONE_CHANGED, … ). • Data URI của dữ liệu cần xử lý và kiểu MIME của dữ liệu đó (ví dụ tel:/216 555-1234 , "http://maps.google.com”, ... ). • Category một string chứa các thông tin bổ sung về loại component cần xử lý intent (ví dụ CATEGORY_BROWSABLE, CATEGORY_LAUNCHER, … ). • Extrascác cặp key-value chứa các thông tin bổ sung cần gửi cho component sẽ xử lý intent. • Flags các cờ thuộc nhiều loại.

  32. Delivering Intents • Một đối tượng Intent được truyền cho Context.startActivity()hoặc Activity.startActivityForResult() để gọi một activity (đồng bộ) hoặc yêu cầu một activity sẵn có làm gì đó (không đồng bộ). • Một đối tượng Intent được truyền cho Context.startService() để bật một service hoặc gửi lệnh mới cho một service đang chạy. • Có thể truyền intent cho Context.bindService() để thiết lập kết nối giữa component đang gọi và component bị gọi. Nó có thể bật service nếu service đó chưa phải đang chạy.

  33. Intent Resolution Có hai loại Intent: • Explicit intentchỉ định component đích bằng tên, thường được dùng khi một activity bật một service con hoặc chạy một activity khác. • Implicit intent không gọi tên component đích (trường dành cho tên component để trống), thường được dùng để kích hoạt các component trong các ứng dụng khác. Late binding được áp dụng ở đây. Mỗi khi có thể, Android sẽ gửi explicit intent cho một thực thể của lớp đích được chỉ định.

  34. Example of Intent (1) • Following fragments calls an Intent whose job is to invoke a built-in task (ACTION_VIEW) and explore the Contacts available in the phone. Intent myIntent = new Intent( Intent.ACTION_VIEW, Uri.parse("content://contacts/people")); startActivity(myIntent);

  35. Example of Intent (1) Intent uses ACTION_VIEW to see Contacts.

  36. Example of Intent (1) • Complete code to see Contacts. package matos.cis493; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; public class AndDemo1 extends Activity { /** show contact list */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent myIntent = new Intent(Intent.ACTION_VIEW,Uri.parse( "content://contacts/people")); startActivity(myIntent); } }

  37. Example of Intent (2) • Following Intent uses built-in task (ACTION_VIEW) to explore a web page (see new Uri value) Intent myIntent = new Intent( Intent.ACTION_VIEW, Uri.parse("http://www.google.com")); startActivity(myIntent); 37

  38. Example of Intent (3) • Following Intent uses built-in task (ACTION_VIEW) to make a phone call (see new Uri value) Intent myIntent = new Intent( Intent.ACTION_VIEW, Uri.parse("tel:/216 555-1234")); startActivity(myIntent); 38

  39. IntentFilters • IntentFilter định nghĩa quan hệ giữa Intent và ứng dụng. • IntentFilter có thể quy định cụ thể đến phần data, hoặc phần action của intent, hoặc cả hai. • IntentFilter còn chứa một trường được gọi là một category. Category giúp phân loại các action. • Ví dụ, category có tên CATEGORY_LAUNCHER chỉ dẫn Android rằng Activity chứa IntentFilter này cần được nhìn thấy tại home screen. 39

  40. IntentFilters • Khi một Intent được phát đi, hệ thống sẽ đánh giá các Activity, Service hiện có, và các BroadcastReceiver đã được đăng kí, rồi gửi Intent đó tới nơi nhận phù hợp nhất (Xem hình sau). 40

  41. IntentFilters 41

  42. IntentFilters • Để báo cho hệ thống biết mình có thể xử lý các implicit intent nào, các activity, service, và broadcast receiver có thể có một hoặc nhiều intent filter. • Mỗi filter mô tả một năng lực mà component đó muốn nhận. • Một explicit intent luôn được gửi đến đích của nó, bất kể nó chứa gì ở trong; không cần hỏi đến các filter. • Nhưng một implicit intent chỉ được gửi đến một component khi và chỉ khi nó lọt qua một trong các filter của component. 42

  43. IntentFilters • IntentFilters are often defined in an application’s AndroidManifest.xml with the <intent-filter> tag. • <intent-filter . . . > • <action android:name="code android.intent.action.MAIN" /> • <category android:name="code android.intent.category.LAUNCHER" /> • <category android:name="android.intent.category.BROWSABLE" /> • <data android:type="video/mpeg" android:scheme="http" . . . /> • <data android:type="audio/mpeg" android:scheme="http" . . . /> • . . . • </intent-filter> 43

  44. Android Applications • Mỗi ứng dụng Android chạy trong tiến trình Linux của riêng nó. • Mỗi ứng dụng bao gồm một tổ hợp các component phần mềm, trong đó có: • Activity • Service • BroadcastReceiver • ContentProvider 44

  45. Android Applications Cấu trúc một ứng dụng Android điển hình 45

  46. Android Services • Service là một component chạy trong background, không tương tác với người dùng, chạy trong khoảng thời gian không xác định. • Mỗi lớp service phải có một khai báo <service>tương ứng trong AndroidManifest.xml. • Service có thể được bật/tắt bằng • Context.startService() và • Context.bindService(). • stopService(…) and unbindService(…)

  47. Android Services • Service, cũng như các đối tượng ứng dụng khác, chạy trong thread chính của tiến trình chủ. • Nghĩa là, nếu một service định làm thao tác gì chiếm nhiều thời gian CPU (chẳng hạn MP3 playback) hoặc blocking (chẳng hạn networking, RSS exchange), nó cần sinh thread riêng để làm công việc đó

  48. Android Services Service1 Class packagematos.service; importandroid.app.Service; importandroid.content.Intent; importandroid.os.IBinder; importandroid.util.Log; publicclass Service1 extends Service implementsRunnable { privateintcounter = 0; @Override publicvoidonCreate() { super.onCreate(); Thread aThread = new Thread(this); aThread.start(); } publicvoid run() { while (true) { try { Log.i("service1", "service1 firing : # " + counter++); Thread.sleep(10000); //this is where the heavy-duty computing occurs } catch (Exception ee) { Log.e("service1", ee.getMessage()); } } } @Override publicIBinderonBind(Intent intent) { returnnull; } }

  49. Android Services // Service1Driver package matos.service; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class Service1Driver extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // invoking the service Intent service1Intent = new Intent( this, Service1.class ); startService( service1Intent ); // do some work here…. } }// Service1Driver

  50. Android Services Service1Demo Manifest <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="matos.service" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Service1Driver" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="Service1" android:enabled="true" > </service> </application> <uses-sdkandroid:minSdkVersion="3" /> </manifest>

More Related