Android Intents

8 215 1
Android Intents

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

Thông tin tài liệu

Android Intents Part 2 Inter-Process Communication Using Bundles Notes are based on: Android Developers http://developer.android.com/index.html 12-2 2 12. AndroidIntents – Part 2 Android intents 2 An activity usually presents a single visual user interface from which a number of actions could be performed. Moving from one activity to another is accomplished by having the current activity start the next one through so called intents. Intent {action + data} requestCode requestResult [ optional data ] Activity-1 startActivityForRe sult … onActivityResult() … Activity-2 onResult() … … 3 12. AndroidIntents – Part 2 IPC Inter-Process Communication 3 Hầu hết các ngôn ngữ lập trình hỗ trợ khái niệm IPC method-calling (gọi phương thức giữa các tiến trình khác nhau) với các tham số truyền theo hai chiều giữa phương thức gọi (caller) và phương thức bị gọi (callee – invoked method). Ở Android, một activity phát lệnh gọi một activity khác bằng cách sử dụng một đối tượng Intent. Lưu ý: Trong Android, caller không dừng lại đợi activity được gọi trả về kết quả. Thay vào đó, nên dùng một phương thức nghe (listening-method) [onActivityResult( .) ]. 4 12. AndroidIntents – Part 2 Android Bundles 4 Thay vì dùng các danh sách tham số/đối số truyền thống, Android dùng cơ chế Intent để xác lập liên lạc giữa các tiến trình (Inter- process-communication - IPC). Để phục vụ mục đích trao đổi dữ liệu, Intent thường mang theo một danh sách các đối số được đặt tên, hay còn gọi là bundle. 5 12. AndroidIntents – Part 2 Android Bundles 5 Cấu trúc Bundle là một cơ chế đơn giản dùng để truyền dữ liệu giữa các activity. Mỗi Bundle là một collection dạng cặp <name, value>. Có một tập các phương thức putXXX và getXXX để lưu vào bundle và lấy ra các giá trị (đơn hoặc mảng) thuộc các kiểu dữ liệu cơ bản. Ví dụ Bundle myBundle = new Bundle(); myBundle.putDouble (“var1”, 3.1415); . Double v1 = myBundle.getDouble(“var1”); Intent myIntentA1A2 = new Intent (Activity1.this, Activity2.class); Bundle myBundle1 = new Bundle(); myBundle1.putInt (“val1”, 123); myIntentA1A2.putExtras(myBundle1); startActivityForResult(myIntentA1A2, 1122); 6 12. AndroidIntents – Part 2 Android Intents & Bundles 6 Activity1: Sender Activity2: Receiver INTENT requestCode (1122) resultCode Extras: { val1 = 123 } Sender class / Receiver class 7 12. AndroidIntents – Part 2 Android Intents & Bundles 7 Activity1: Sender Activity2: Receiver INTENT requestCode (1122) resultCode Extras: { val1 = 123 } Sender class / Receiver class Intent myLocalIntent2 = getIntent(); Bundle myBundle = myLocalIntent.getExtras(); int val1 = myBundle.getInt(“val1"); 8 12. AndroidIntents – Part 2 Android Intents & Bundles 8 Activity1: Sender Activity2: Receiver INTENT requestCode (1122) resultCode (OK) Extras: { val1 = 456 } Sender class / Receiver class myBundle.putString("val1", 456 ); myLocalIntent.putExtras(myBundle); setResult(Activity.RESULT_OK, myLocalIntent);

Ngày đăng: 03/10/2013, 01:01

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

Tài liệu liên quan