Android Fundamentals

13 143 0
Android Fundamentals

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Bài này sẽ hướng dẫn cặn kẽ mọi người cách sử dụng Android trong Eclipse, hi vọng giúp những người mới chập chững bước vào lập trình Android có thêm thông tin và biết cách khai thác IDE của mình hiệu quả hơn. I.Hướng dẫn cài đặt Android với Eclipse: Thực chất anh Giáp đã viết một bài có nội dung tương tự, nhưng do Goolge thay đổi phương thức tải Android SDK nên mình quyết định viết lại, gộp luôn vào topic hướng dẫn sử dụng Eclipse. 1.Download Android SDK: Android SDK thực chất là tập hợp các công cụ và thư viện để phát triển các ứng dụng trên nền tảng hệ điều hành Android. B1: Vào trang http://developer.android.com/sdk/index.html để tải Android SDK Starter. Tùy thuộc vào hệ điều hành mà bạn chọn bản Mac, Linux hay Window. Ở đây mình chọn tải bản cho Window. B2: Giải nén file zip bạn vừa tải về. Chạy SDK Setup.exe. Bạn có thể gặp thông báo lỗi Fetching https://dl-sl . Failed to fetch . Close thông báo này lại. Tiếp theo cửa sổ Choose Packages to Install xuất hiện. Nếu cửa sổ này trống rỗng -> Cancel. -> Quay về cửa sổ Android SDK and AVD manager -> Chọn Setting, đánh dấu vào ô Force https:// . -> Chọn Available Packages B3: Đánh dấu các Packages bạn muốn tải: Documents chính là phần Javadoc mô tả hoạt động của các phương thức và các lớp (phần này chắc chắn không thể thiếu rồi), Sample là các đoạn code mẫu, SDK Platform ứng với các phiên bản hệ điều hành (2.2 - API level 8, 2.1 - API level 7, .), và Google API để phát triển các phần mềm liên quan đến dịch vụ của Google (như Google Map nếu bạn muốn lập trình liên quan đến GPS). Các bạn có thể tải hết nếu thích, còn muốn tối ưu thì có thể đánh dấu như mình (lưu ý USB drivers chỉ dành cho người sử dụng Windows và muốn phát triển ứng dụng test bằng điện thoại thật). -> Install Selected -> Install -> Cửa sổ Install hiện ra -> Ngồi chờ (>_<) 2.Tích hợp Android SDK vào Eclipse: B1: Tải Eclipse nếu bạn chưa có. Mọi người có thể phân vân không biết tải bản nào cho phù hợp, nhưng theo ý kiến của mình thì có thể dùng 1 trong 2 bản sau: Eclipse for Java Developers, hoặc Eclipse for Java and Report Developers (mình dùng bản sau). B2: Khởi chạy Eclipse, vào Help -> Install new softwares. Chọn Add, gõ vào ô Name tên bạn muốn và Location gõ vào địa chỉ để tải về ADT: HTML Code: https://dl-ssl.google.com/android/eclipse/ hoặc HTML Code: http://dl-ssl.google.com/android/eclipse/ nếu https không hoạt động. Ngoài ra bạn cũng có thể tải thẳng ADT về máy theo link http://dl.google.com/android/ADT-0.9.7.zip (bản mới nhất 0.9.7 ứng với Android 2.2), chọn Archive và browse tới file này (lưu ý không giải nén) -> OK -> Check vào phần dưới ô Name (sẽ hiện ra dòng Developer Tools). B3: Next, next, Accept, next, .Finish (như Install mọi chương trình bình thường). B4: Eclipse -> Windows -> Preferences -> Android Nhấn nút Browse và chỉnh đường dẫn tới thư mục của Android SDK bạn tải lúc trước. -> Apply -> OK Android Introduction Application Fundamentals @2011 Mihail L Sichitiu Goal  Understand applications and their components  Concepts:       activity, service, broadcast receiver, content provider, intent, AndroidManifest @2011 Mihail L Sichitiu Applications  Written in Java (it’s possible to write native code – will not cover that here)  Good separation (and corresponding security) from other applications:    Each application runs in its own process Each process has its own separate VM Each application is assigned a unique Linux user ID – by default files of that application are only visible to that application (can be explicitly exported) @2011 Mihail L Sichitiu Application Components  Activities – visual user interface focused on a single thing a user can  Services – no visual interface – they run in the background  Broadcast Receivers – receive and react to broadcast announcements  Content Providers – allow data exchange between applications @2011 Mihail L Sichitiu Activities  Basic component of most applications  Most applications have several activities that start each other as needed  Each is implemented as a subclass of the base Activity class @2011 Mihail L Sichitiu Activities – The View  Each activity has a default window to draw in (although it may prompt for dialogs or notifications)  The content of the window is a view or a group of views (derived from View or ViewGroup)  Example of views: buttons, text fields, scroll bars, menu items, check boxes, etc  View(Group) made visible via Activity.setContentView() method @2011 Mihail L Sichitiu Services  Does not have a visual interface  Runs in the background indefinitely  Examples     Network Downloads Playing Music TCP/UDP Server You can bind to a an existing service and control its operation @2011 Mihail L Sichitiu Broadcast Receivers  Receive and react to broadcast announcements  Extend the class BroadcastReceiver  Examples of broadcasts:   Low battery, power connected, shutdown, timezone changed, etc Other applications can initiate broadcasts @2011 Mihail L Sichitiu Content Providers  Makes some of the application data available to other applications  It’s the only way to transfer data between applications in Android (no shared files, shared memory, pipes, etc.)  Extends the class ContentProvider;  Other applications use a ContentResolver object to access the data provided via a ContentProvider @2011 Mihail L Sichitiu Intents   An intent is an Intent object with a message content Activities, services and broadcast receivers are started by intents ContentProviders are started by ContentResolvers:    An activity is started by Context.startActivity(Intent intent) or Activity.startActivityForResult(Intent intent, int RequestCode) A service is started by Context.startService(Intent service) An application can initiate a broadcast by using an Intent in any of Context.sendBroadcast(Intent intent), Context.sendOrderedBroadcast(), and Context.sendStickyBroadcast() @2011 Mihail L Sichitiu 10 Shutting down components  Activities    Services   Can terminate via stopSelf(); or Context.stopService(); Content Providers   Can terminate itself via finish(); Can terminate other activities it started via finishActivity(); Are only active when responding to ContentResolvers Broadcast Receivers  Are only active when responding to broadcasts @2011 Mihail L Sichitiu 11 Android Manifest  Its main purpose in life is to declare the components to the system: @2011 Mihail L Sichitiu 12 Intent Filters  Declare Intents handled by the current application (in the AndroidManifest): Shows in the Launcher and Handles JPEG images in some way @2011 Mihail L Sichitiu 13 JavaJavaFundamentalsFundamentalsthangld@uit.edu.vnthangld@uit.edu.vnKhoa Mạng máy tính và Truyền thôngKhoa Mạng máy tính và Truyền thôngĐại học Công nghệ Thông tinĐại học Công nghệ Thông tin Nội dungNội dung Giới thiệu JavaGiới thiệu Java Ứng dụng JavaỨng dụng Java OOP trong JavaOOP trong Java Wrapper ClassesWrapper Classes StringString Exception HandlingException Handling Nhập/xuất dữ liệuNhập/xuất dữ liệu Giới thiệu Java [1]Giới thiệu Java [1] Ngôn ngữ lập trình hướng đối tượngNgôn ngữ lập trình hướng đối tượng Ngôn ngữ thông dịchNgôn ngữ thông dịch Độc lập hệ nền (MultiĐộc lập hệ nền (Multi--platform / Platformplatform / Platform--Independent)Independent)Independent)Independent) Giới thiệu Java [2]Giới thiệu Java [2] Ngôn ngữ giống C/C++Ngôn ngữ giống C/C++ Không có khái niệm con trỏKhông có khái niệm con trỏ Hủy đối tượng tự độngHủy đối tượng tự động Biến môi trường CLASSPATH: chỉ đến Biến môi trường CLASSPATH: chỉ đến thư mục / zip file / jar file chứa các class thư mục / zip file / jar file chứa các class thư việnthư viện Java Development KitJava Development Kit Bộ công cụ phát triển Java (Windows)Bộ công cụ phát triển Java (Windows)Thư mục <j2sdk_home>/binThư mục <j2sdk_home>/binjavac.exejavac.exe: Java Compiler: Java Compiler javac <java_source_file.java>javac <java_source_file.java> javac <java_source_file.java>javac <java_source_file.java>java.exejava.exe: Java Interpreter: Java Interpreter java <java_class_file.class>java <java_class_file.class> Thực thi ứng dụng JavaThực thi ứng dụng JavaCompileCompileJava Source FileJava Source File(.java)(.java)Ứng dụng JavaỨng dụng JavaJava APIJava APIMáy ảo JavaMáy ảo Java (JVM)(JVM)Hệ thống phần cứng máy tínhHệ thống phần cứng máy tínhJava Class FileJava Class File(.class)(.class)InterpretInterpret Ứng dụng JavaỨng dụng JavaHelloWorldApp.javaHelloWorldApp.javapublic class HelloWorldApp{public class HelloWorldApp{public static void main(String[] args){public static void main(String[] args){System.out.println(“HelloWorld”);System.out.println(“HelloWorld”);System.out.println(“HelloWorld”);System.out.println(“HelloWorld”);}}}} Kiểu dữ liệuKiểu dữ liệu Primitive TypesPrimitive Typesbytebytecharcharbooleanboolean Reference TypesReference Typesarrayarrayclassclassinterfaceinterfacebooleanbooleanshortshortintintlonglongfloatfloatdoubledoubleinterfaceinterface Chuyển đổi kiểu dữ liệu [1]Chuyển đổi kiểu dữ liệu [1] Một kiểu dữ liệu được chuyển đổi sang Một kiểu dữ liệu được chuyển đổi sang một kiểu dữ liệu khácmột kiểu dữ liệu khác Ví dụVí dụfloat c = 34.89675f;float c = 34.89675f;float c = 34.89675f;float c = 34.89675f;int b = (int)c + 10;int b = (int)c + 10;c = b;c = b; Có hai cách chuyển đổi kiểu dữ liệu: tự Có hai cách chuyển đổi kiểu dữ liệu: tự động và ép kiểuđộng và ép kiểu Chuyển đổi kiểu dữ liệu [2]Chuyển đổi kiểu dữ liệu [2] Khi dữ liệu ,với một kiểu dữ liệu cho Khi dữ liệu ,với một kiểu dữ liệu cho trước, được gán cho một biến có kiểu dữ trước, được gán cho một biến có kiểu dữ liệu khác, quá trình chuyển đổi kiểu dữ liệu khác, quá trình chuyển đổi kiểu dữ liệu tự động thực hiện nếu thõa các điều liệu tự động thực hiện nếu thõa các điều liệu tự động thực hiện nếu thõa các điều liệu tự động thực hiện nếu thõa các điều kiện sau:kiện sau:Hai kiểu dữ liệu tương thích nhauHai kiểu dữ liệu tương thích nhauKiểu dữ liệu đích lớn hơn kiểu dữ liệu nguồnKiểu dữ liệu đích lớn hơn kiểu dữ liệu nguồn Ép kiểu dữ liệu là sự chuyển đổi dữ liệu Ép kiểu dữ liệu là sự chuyển đổi dữ liệu tường minh. Nó có thể làm mất thông tintường minh. Nó có thể làm mất thông tin [...]... (in the AndroidManifest): Shows in the Launcher and Handles JPEG images in some way @2011 Mihail L Sichitiu ... android: icon="@drawable/small_pic.png" activity to android: label="@string/freneticLabel" start >

Ngày đăng: 22/04/2016, 10:09

Mục lục

  • Android Introduction

  • Goal

  • Applications

  • Application Components

  • Activities

  • Activities – The View

  • Services

  • Broadcast Receivers

  • Content Providers

  • Intents

  • Shutting down components

  • Android Manifest

  • Intent Filters

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan