1. Trang chủ
  2. » Công Nghệ Thông Tin

Hướng dẫn Lập trình Android cơ bản

140 217 1

Đ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 1: Tổng quan Hệ điều hành Android Kiến trúc Đặc điểm Các phiên Phát triển ứng dụng Android Android API Environment setup Giới thiệu Android OS Hệ điều hành mã nguồn mở Dựa Linux Dành cho thiết bị di động Được phát triển OHA (Open Handset Alliance) Source: GNU General Public License Mobile OS sử dụng nhiều (1 tỉ thiết bị, khắp gần 200 quốc gia) Giới thiệu Kiến trúc Bao gồm software component 4 tầng Application Application framework Libraries Linux kernel Kiến trúc Linux kernel 3.x Tầng trừu tượng phần cứng driver (camera, keypad, display) Hỗ trợ networking tốt, nhiều driver loại thiết bị khác Libraries Open-source Web browser engine WebKit SQLite database Play and record audio and video SSL responsible for Internet security … Android Runtime: Dalvik Virtual Machine Quản lý nhớ Đa luồng Giúp lập trình viên sử dụng ngôn ngữ Java chuẩn Libraries android.app android.content android.database android.opengl android.os android.text android.view android.widget android.webkit Application Framework High level service (java class) Gồm: Activity Manager: quản lý vòng đời ứng dụng activity Content Providers: publish share liệu với ứng dụng khác Resource Manager: truy cập, quản lý tài nguyên: chuỗi, thiết lập màu, layout,… Notifications Manager: hiển thị cảnh báo, thông báo View System: tạo UI Implicit Intent Implicit Intent: không định rõ thành phần xử lý, thay vào bổ sung thuộc tính như: mô tả hành động, dạng liệu… Thường sử dụng để kích hoạt thành phần ứng dụng khác Intent read1=new Intent(); read1.setAction(android.content.Intent.ACTION_VIEW); read1.setData(ContactsContract.Contacts.CONTENT_URI); startActivity(read1); Tạo Intent Đối tượng Intent khởi động thành phần ứng dụng đồng thời mang thông tin liệu xử lý, bao gồm thành phần sau: Component: tên thành phần nhận xử lý Intent Action: hành động yêu cầu thực thi Data: liệu yêu cầu nhận xử lý Category: mô tả lĩnh vực hoạt động Extras: key/value cho phép gửi nhận thông tin Flag: biến cờ mô tả cách thức hoạt động Tạo Explicit Intent Chỉ cần sử dụng thuộc tính Component Khai báo: Intent intent = new Intent(this, ); Ví dụ: khởi động Activity có tên SecondActivity từ MainActivity Intent intent = new Intent(MainActivty.this, SecondActivity.class); startActivity(intent); Tạo implicit Intent Chỉ cần sử dụng thuộc tính Action Khai báo: Intent intent = new Intent(); Ví dụ: khởi động Activity thực ACTION_VIEW Intent intent = new Intent(Intent.ACTION_VIEW); startActivity(intent); Tạo Intent Một số Action thường dùng Intent ACTION_VIEW ACTION_DIAL ACTION_CALL ACTION_EDIT ACTION_DELETE ACTION_SEND ACTION_SENDTO Tạo Intent Data: dạng đường dẫn URI, cho phép trỏ đến bảng liệu truy xuất thông tin bao gồm: type scheme + authority + path Data định thông qua ba phương thức: setData setType setDataAndType Ví dụ: thực gọi thông qua liệu số điện thoại Intent callPhone = new Intent(Intent.ACTION_CALL); callPhone.setData(Uri.parse(“tel:01234-56789”)); startActivity(callPhone); Tạo Intent Extras Cặp key – value để bổ sung thông tin gởi đến thành phần quản lý intent Có thể ghi/đọc thông qua phương thức putExtras() getExtras() Có hai cách gửi liệu vào Intent: Trực tiếp: Dùng phương thức putExtra(Key, Value) thiết lập trực tiếp vào Intent Intent intent = new Intent(); intent.putExtra(“SoNguyenX”, x); Thông qua Bundle o Tạo đối tượng Bundle, dùng phương thức set(Key, Value) vào đối tượng Bundle o Dùng phương thức putExtras() gửi Bundle vào Intent Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putInt(“SoNguyenX”, x); intent.putExtras(bundle); Truy xuất Truy xuất liệu trực tiếp Extras: Dùng phương thức getExtra(Key, DefaultValue) để truy xuất liệu Intent Intent intent = getIntent(); int soNguyenX = intent.getIntExtra(“SoNguyenX”, 0); Thông qua Bundle Dùng phương thức getExtras() để truy xuất đối tượng Bundle Intent Dùng phương thức get(Key, DefaultValue) để truy xuất liệu Bundle Intent intent = getIntent(); Bundle bundle = intent.getExtras(); int soNguyenX = bundle.getInt(“SoNguyenX”, 0); Intent Filter Mô tả cấu trúc Intent, cho phép thực nhận Intent theo cấu trúc mô tả Lọc Intent theo ba thuộc tính Action Data (type, scheme, authority & path) Category Intent Filter IntentFilter thực lọc Intent theo thứ tự ưu tiên có nhiều thuộc tính thiết lập có qui tắc định: Nếu không thiết lập Action, nhận Intent Action Nếu thiết lập thuộc tính Action không thiết lập thuộc tính Data, cho phép lọc Intent Data IntentFilter cho phép nhận Intent có liệu có liên quan đến thuộc tính Action Xây dựng Intent Filter Có thể khởi tạo đối tượng IntentFilter cách: Trong java Code: o Các hàm khởi tạo: o IntentFilter() o IntentFilter(String Action) o IntentFilter(String Action, URI data) o IntentFilter(IntentFilter o) Trong tập tin AndroidManifest.xml: o Khai báo thẻ cặp thẻ o Trong cặp thẻ chứa thẻ: Xây dựng Intent Filter Action: Các thuộc tính: Trong đó: android:name : sử dụng thuộc tính lớp Intent.ACTION_string tự định nghĩa chuỗi action Vd: Xây dựng Intent Filter Data: Các thuộc tính Vd: Xây dựng Intent Filter Category: Các thuộc tính: Trong đó: o android:name : Intent.CATEGORY_string Khai báo theo cấu trúc android.intent.category.string Ví dụ khai báo VD b1=(Button)findViewById(R.id.button); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.example.com")); startActivity(i); } }); b2=(Button)findViewById(R.id.button2); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent (android.content.Intent.ACTION_VIEW,Uri.parse("tel:9510300000")); startActivity(i); } }); [...]... Multi-Language GCM: Google Cloud Messaging Wi-Fi Direct Android Beam: NFC-based technology Phiên bản Đặt tên: A - M First beta (SDK): released by Google – 2007 1.0: 2008 6/2012: 4.1 (Jelly Bean) – phiên bản cải tiến hướng đến UI, tính năng, hiệu năng Phiên bản Ứng dụng Android Lập trình Android Bộ công cụ phát triển SDK Ngôn ngữ lập trình chính là Java API Đóng gói và phân phối dễ dàng Google... Mobango,F-droid Amazon Appstore Công cụ phát triển Java JDK Android SDK Java Runtime Environment (JRE) Android Studio Eclipse IDE for Java Developers (optional) Android Development Tools (ADT) Eclipse Plug-in (optional) Android API Platform Version Android 5.1 Android 5.0 Android 4.4W Android 4.4 Android 4.3 API Level 22 21 20 19 18 Android 4.2, 4.2.2 17 Android 4.1, 4.1.1 16 VERSION_CODE LOLLIPOP_MR1 LOLLIPOP... 4.2.2 17 Android 4.1, 4.1.1 16 VERSION_CODE LOLLIPOP_MR1 LOLLIPOP KITKAT_WATCH KITKAT JELLY_BEAN_MR 2 JELLY_BEAN_MR 1 JELLY_BEAN Android API Platform Version Android 4.0, 4.0.1, 4.0.2 Android 3.2 API Level 14 13 Android 3.1.x 12 Android 3.0.x Android 2.3.4 Android 2.3.3 Android 2.3.2 11 10 9 VERSION_CODE ICE_CREAM_SAN DWICH HONEYCOMB_MR 2 HONEYCOMB_MR 1 HONEYCOMB GINGERBREAD_ MR1 GINGERBREAD Environment... tả các đặc điểm của ứng dụng Định nghĩa các thành phần MainActivity.java Converted to a Dalvik executable package com.example.helloworld; import android. os.Bundle; import android. app.Activity; import android. view.Menu; import android. view.MenuItem; import android. support.v4.app.NavUtils; public class MainActivity extends Activity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);... Mac OS X 10.5.8 (Intel chip) Linux theo GNU C Library 2.7 trở đi Bài 2: Các thành phần ứng dụng Android 1 Activities 2 Services 3 Broadcast Receivers 4 Content Providers 5 Khác 6 Hello World example (Android Studio và tổ chức resource) Application Components Các thành phần xây dựng chương trình Gắn với AndroidManifest.xml (mô tả các thành phần ứng dụng và cách thức tương tác) Main component: Activities:... -> New -> Project -> Android New Application Workspace Explorer Explorer 1 2 src: java file chứa activity class (MainActivity.java) gen:   3 bin:  4 5 6 Chứa apk (ADT build) res/drawable-hdpi: chứa drawable object res/layout: user interface res/values:   7 .R file do trình biên dịch tạo ra Tham chiếu đến các resource của project XML file Chứa resource: string, định nghĩa màu AndroidManifest.xml:... AndroidManifest.xml (mô tả các thành phần ứng dụng và cách thức tương tác) Main component: Activities: điều khiển UI Services: background processing + application Broadcast Receivers: quản lý giao tiếp giữa Android OS và applications Content Providers: quản lý dữ liệu Khác: Views, Layouts, Intents, Resources, Manifest,… Activities Single screen with a user interface shows a list of new emails compose... R.layout.activity_main tham chiếu đến activity_main.xml (res/layout) onCreate(): được gọi khi activity được load The Manifest File Thuộc thư mục root của ứng dụng Chứa khai báo các thành phần ứng dụng Giao tiếp giữa android OS và ứng dụng The Manifest File Strings File strings.xml thuộc res/values Chứa các chuỗi liên quan ứng dụng: tên button, label, default text, Ex: file strings.xml mặc định

Ngày đăng: 30/05/2016, 22:34

Xem thêm: Hướng dẫn Lập trình Android cơ bản

TỪ KHÓA LIÊN QUAN

w