Bài giảng Lập trình cho thiết bị di động: Chương 4 - ĐH Đồng Nai
Số trang: 147
Loại file: pptx
Dung lượng: 5.96 MB
Lượt xem: 18
Lượt tải: 0
Xem trước 10 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Bài giảng Lập trình cho thiết bị di động: Chương 4 - Xử lý đa tiến trình và dịch vụ trình bày các nội dung về Multi - Threading, Intent filter và một số nội dung khác.
Nội dung trích xuất từ tài liệu:
Bài giảng Lập trình cho thiết bị di động: Chương 4 - ĐH Đồng Nai DONG NAI UNIVERSITY OF TECHNOLOGY1. Multi - Threading 2. Intent filter 3. Broadcast Receiver 4. Android Services 5. Webservice .Net 1 DONG NAI UNIVERSITY OF TECHNOLOGY1. Multi - Threading 1.1 Introduction 1.2 Handler class 1.3 AsyncTask class 2 DONG NAI UNIVERSITY OF TECHNOLOGY1.1 IntroductionThreads Android’s threads run in a manner similar to common Java threads A Thread is a concurrent unit of execution. not executing in order has its own call stack for methods being invoked, their arguments and local variables. Each virtual machine instance has at least one main Thread running when it is started; The application might decide to launch additional Threads for specific purposes. 3 DONG NAI UNIVERSITY OF TECHNOLOGY 1.1 IntroductionMulti- Threading 4 DONG NAI UNIVERSITY OF TECHNOLOGY1.1 IntroductionMulti- ThreadingThreads in the same VM interact and synchronize by theuse of shared objects and monitors associated withthese objects.There are basically two main ways of having a Threadexecute application code. 1.Create a new class that extends Thread and override its run() method. 2.Create a new Thread instance passing to it a Runnable object.Inboth cases, the start() method must be called toactually execute the new Thread. 5 DONG NAI UNIVERSITY OF TECHNOLOGY1.1 IntroductionAdvantages of Multi- ThreadingThreads share the process resources but are able toexecute independently.Applications responsibilities can be separated mainthread runs UI, and slow tasks are sent to backgroundthreads.Threading provides an useful abstraction of concurrentexecution.Particularly useful in the case of a single process thatspawns multiple threads on top of a multiprocessorsystem. In this case real parallelism is achieved. Consequently,a multithreaded program operates faster on computer systems that have multiple CPUs. 6 DONG NAI UNIVERSITY OF TECHNOLOGY1.1 IntroductionDisadvantages of Multi- ThreadingCode :more complexNeed to detect, avoid, resolve deadlocks Threadsnot executing in order Runnable v.s Thread? What different? Deadlock and Atomic type 7 DONG NAI UNIVERSITY OF TECHNOLOGY1.2 Handler classAn application may involve a time-consuming operation,however we want the UI to be responsive to the user. Androidoffers two ways for dealing with this scenario: Do expensive operations in a background service, using notifications to inform users about next step Do the slow work in a background thread. Interaction between Android threads is accomplished using (a) Handler objects and (b) posting Runnable objects to the main view. 8 DONG NAI UNIVERSITY OF TECHNOLOGY1.2 Handler classWhen a process is created for your application, its mainthread is dedicated to running a message queue that takescare of managing the top-level application objects (activities,intent receivers, etc) and any windows they create.You can create your own secondary threads, andcommunicate back with the main application thread througha Handler.When you create a new Handler, it is bound to themessage queue of the thread that is creating it -- from thatpoint on, it will deliver messages and runnables to thatmessage queue and execute them as they come out of themessage queue. 9 DONG NAI UNIVERSITY OF TECHNOLOGY1.2 Handler class There are two main uses for a Handler: (1)to schedule messages and runnables to be executed as some point in the future; and (2)to enqueue an action to be performed on another thread 10 DONG NAI UNIVERSITY OF TECHNOLOGY1.2 Handler classThreads and UI Warning Background threads are not allowed to interact with the UI. Only the main process can access the (main) activity’s view. (Global) class variables can be seen and updated in the threads 11 DONG NAI UNIVERSITY OF TECHNOLOGY1.2 ...
Nội dung trích xuất từ tài liệu:
Bài giảng Lập trình cho thiết bị di động: Chương 4 - ĐH Đồng Nai DONG NAI UNIVERSITY OF TECHNOLOGY1. Multi - Threading 2. Intent filter 3. Broadcast Receiver 4. Android Services 5. Webservice .Net 1 DONG NAI UNIVERSITY OF TECHNOLOGY1. Multi - Threading 1.1 Introduction 1.2 Handler class 1.3 AsyncTask class 2 DONG NAI UNIVERSITY OF TECHNOLOGY1.1 IntroductionThreads Android’s threads run in a manner similar to common Java threads A Thread is a concurrent unit of execution. not executing in order has its own call stack for methods being invoked, their arguments and local variables. Each virtual machine instance has at least one main Thread running when it is started; The application might decide to launch additional Threads for specific purposes. 3 DONG NAI UNIVERSITY OF TECHNOLOGY 1.1 IntroductionMulti- Threading 4 DONG NAI UNIVERSITY OF TECHNOLOGY1.1 IntroductionMulti- ThreadingThreads in the same VM interact and synchronize by theuse of shared objects and monitors associated withthese objects.There are basically two main ways of having a Threadexecute application code. 1.Create a new class that extends Thread and override its run() method. 2.Create a new Thread instance passing to it a Runnable object.Inboth cases, the start() method must be called toactually execute the new Thread. 5 DONG NAI UNIVERSITY OF TECHNOLOGY1.1 IntroductionAdvantages of Multi- ThreadingThreads share the process resources but are able toexecute independently.Applications responsibilities can be separated mainthread runs UI, and slow tasks are sent to backgroundthreads.Threading provides an useful abstraction of concurrentexecution.Particularly useful in the case of a single process thatspawns multiple threads on top of a multiprocessorsystem. In this case real parallelism is achieved. Consequently,a multithreaded program operates faster on computer systems that have multiple CPUs. 6 DONG NAI UNIVERSITY OF TECHNOLOGY1.1 IntroductionDisadvantages of Multi- ThreadingCode :more complexNeed to detect, avoid, resolve deadlocks Threadsnot executing in order Runnable v.s Thread? What different? Deadlock and Atomic type 7 DONG NAI UNIVERSITY OF TECHNOLOGY1.2 Handler classAn application may involve a time-consuming operation,however we want the UI to be responsive to the user. Androidoffers two ways for dealing with this scenario: Do expensive operations in a background service, using notifications to inform users about next step Do the slow work in a background thread. Interaction between Android threads is accomplished using (a) Handler objects and (b) posting Runnable objects to the main view. 8 DONG NAI UNIVERSITY OF TECHNOLOGY1.2 Handler classWhen a process is created for your application, its mainthread is dedicated to running a message queue that takescare of managing the top-level application objects (activities,intent receivers, etc) and any windows they create.You can create your own secondary threads, andcommunicate back with the main application thread througha Handler.When you create a new Handler, it is bound to themessage queue of the thread that is creating it -- from thatpoint on, it will deliver messages and runnables to thatmessage queue and execute them as they come out of themessage queue. 9 DONG NAI UNIVERSITY OF TECHNOLOGY1.2 Handler class There are two main uses for a Handler: (1)to schedule messages and runnables to be executed as some point in the future; and (2)to enqueue an action to be performed on another thread 10 DONG NAI UNIVERSITY OF TECHNOLOGY1.2 Handler classThreads and UI Warning Background threads are not allowed to interact with the UI. Only the main process can access the (main) activity’s view. (Global) class variables can be seen and updated in the threads 11 DONG NAI UNIVERSITY OF TECHNOLOGY1.2 ...
Tìm kiếm theo từ khóa liên quan:
Thiết bị di động Kỹ thuật lập trình Điện thoại di động Lập trình điện thoại Giao diện người dùng Xử lý tập tinTài liệu có liên quan:
-
Bài giảng Phân tích thiết kế hệ thống thông tin: Chương 3 - Hệ điều hành Windowns XP
39 trang 386 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 310 0 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 248 0 0 -
43 trang 224 0 0
-
Giới thiệu môn học Ngôn ngữ lập trình C++
5 trang 222 0 0 -
Bài giảng Nhập môn về lập trình - Chương 1: Giới thiệu về máy tính và lập trình
30 trang 189 0 0 -
72 trang 181 0 0
-
Luận văn: Nghiên cứu kỹ thuật giấu tin trong ảnh Gif
33 trang 159 0 0 -
Đồ án tốt nghiệp ngành Công nghệ thông tin: Lập trình game trên thiết bị di động
56 trang 157 0 0 -
33 trang 147 0 0