Bài giảng lập trình cho thiết bị di động chương 4 đh đồng nai

147 610 0
Bài giảng lập trình cho thiết bị di động  chương 4   đh đồng nai

Đ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

DONG NAI UNIVERSITY OF TECHNOLOGY Multi - Threading Intent filter Broadcast Receiver Android Services Webservice Net DONG NAI UNIVERSITY OF TECHNOLOGY Multi - Threading 1.1 Introduction 1.2 Handler class 1.3 AsyncTask class DONG NAI UNIVERSITY OF TECHNOLOGY 1.1 Introduction Threads 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 DONG NAI UNIVERSITY OF TECHNOLOGY 1.1 Introduction Multi- Threading DONG NAI UNIVERSITY OF TECHNOLOGY 1.1 Introduction Multi- Threading Threads in the same VM interact and synchronize by the use of shared objects and monitors associated with these objects There In are basically two main ways of having a Thread execute 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 both cases, the start() method must be called to actually execute the new Thread DONG NAI UNIVERSITY OF TECHNOLOGY 1.1 Introduction Advantages of Multi- Threading Threads share the process' resources but are able to execute independently Applications responsibilities can be separated main thread runs UI, and slow tasks are sent to background threads Threading provides an useful abstraction of concurrent execution Particularly useful in the case of a single process that spawns multiple threads on top of a multiprocessor system In this case real parallelism is achieved Consequently, a multithreaded program operates faster on computer systems that have multiple CPUs DONG NAI UNIVERSITY OF TECHNOLOGY 1.1 Introduction Disadvantages of Multi- Threading Code :more complex Need to detect, avoid, resolve deadlocks Threads not executing in order Runnable What Deadlock v.s Thread? different? and Atomic type DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Handler class An application may involve a time-consuming operation, however we want the UI to be responsive to the user Android offers 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 DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Handler class When a process is created for your application, its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, intent receivers, etc) and any windows they create You can create your own secondary threads, and communicate back with the main application thread through a Handler When you create a new Handler, it is bound to the message queue of the thread that is creating it from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue DONG NAI UNIVERSITY OF TECHNOLOGY 1.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 TECHNOLOGY 5.5 Android to Net Webservice Example 3: get complex data Use the getListCatalog() method in Webservice 133 DONG NAI UNIVERSITY OF TECHNOLOGY 5.5 Android to Net Webservice Example 3: cont 134 DONG NAI UNIVERSITY OF TECHNOLOGY 5.5 Android to Net Webservice Example 3: cont 135 DONG NAI UNIVERSITY OF TECHNOLOGY 5.5 Android to Net Webservice Example 3: cont 136 DONG NAI UNIVERSITY OF TECHNOLOGY 5.5 Android to Net Webservice Example 4: get complex data Use the getListProductbyCateId () method in Webservice Take yourself (exercise) 137 DONG NAI UNIVERSITY OF TECHNOLOGY 5.5 Android to Net Webservice Example 5: Insert SoabObject to webservice Now in Net, I add a new method with name insertCatalog, parameter is a catalog object 138 DONG NAI UNIVERSITY OF TECHNOLOGY 5.5 Android to Net Webservice Example 5: cont Also, add execNonquery method for Connectionfactory class 139 DONG NAI UNIVERSITY OF TECHNOLOGY 5.5 Android to Net Webservice Example 5: cont Please see the Soap description for insertCatalog method: 140 DONG NAI UNIVERSITY OF TECHNOLOGY 5.5 Android to Net Webservice Example 5: cont SoapObject request=new SoapObject(NAMESPACE, METHOD_NAME); SoapObject newCate=new SoapObject(NAMESPACE, "cate"); newCate.addProperty("CateId", "cate4"); newCate.addProperty("CateName", "hoa chat"); request.addSoapObject(newCate); 141 DONG NAI UNIVERSITY OF TECHNOLOGY 5.5 Android to Net Webservice Example 5: cont 142 DONG NAI UNIVERSITY OF TECHNOLOGY 5.5 Android to Net Webservice Example 5: cont Coding to insert a new catalog 143 DONG NAI UNIVERSITY OF TECHNOLOGY 5.5 Android to Net Webservice Example 5: cont 144 DONG NAI UNIVERSITY OF TECHNOLOGY 5.5 Android to Net Webservice Example 6: how to connect real Android mobile Device with Computer Server (by Wireless) Many ways: 1) Using Portable wifi hotspot (AndroidAp active)- android mobile device has supports (you should use this feature) 2) Connectivity software and other software (The PC must use Win7, Win8) 3) Internet global 145 DONG NAI UNIVERSITY OF TECHNOLOGY Example 6: cont 5.5 Android to Net Webservice The figure I captured by Phone& Laptop Camera Database + webservice in My Labtop Here is my real Phone: SamSung S2 Insert Cate Get list Cate Exercise: You must that 146 DONG NAI UNIVERSITY OF TECHNOLOGY END 147 [...]... or request the execution of runnable objects through the post() method 12 DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Handler class  Handler’s MessageQueue 13 DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Handler class Using Messages Main Thread Background Thread … … … … 14 DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Handler class 15 DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Handler class  To send Messages a Message to... quickly, as other UI work is suspended until the Handler is done 18 DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Handler class   XML Layout Examples – Using Message 19 DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Handler class  Coding msg get from sendMessage in background thread 20 DONG NAI UNIVERSITY OF TECHNOLOGY  1.2 Handler class Coding msg get from main Thread msg send to main Thread, process in handleMessage... handleMessage We could use msg.arg1, msg.arg2, msg.obj (store Object) , msg.what to process message 21 DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Handler class  Examples – using post 22 DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Handler class 23 DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Handler class 24 DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Handler class Exercise: Draw Button at runtime on the View as below Whe click... click on the “Draw Button:”: - After 1 second, application will draw 1 button The number of button is entered in the EditText - Must use MultiThreading (Message or post) 25 DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 AsyncTask class 26 DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 AsyncTask class 27 DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 AsyncTask class Start by execute method AsyncTask enables proper and easy use of... class 31 DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 AsyncTask class  Examples : combine AsyncTask and Handler class Random Number: Auto draw button with random number in the left side List prime: Auto draw list Prime butto when the Random number is finished in the Right side 32 DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 AsyncTask class 33 DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 AsyncTask class 34 DONG NAI UNIVERSITY... the UI thread An asynchronous task is defined by 28 DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 AsyncTask class AsyncTask Not all types are always used by an asynchronous task To mark a type as unused, simply use the type Void Note: Syntax “String ” indicates (Varargs) array of String values, similar to “String[]” 29 DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 AsyncTask class onPreExecute(),... sendMessage Method You deliver the message using one of the sendMessage () family of methods, such as … •sendMessage() puts the message at the end of the queue immediately • sendMessageAtFrontOfQueue() puts the message at the front of the queue immediately (versus the back, as is the default), so your message takes priority over all others • sendMessageAtTime() puts the message on the queue at the stated...DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Handler class Threads and UI Warning Background Only threads are not allowed to interact with the UI 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 TECHNOLOGY 1.2 Handler class  A secondary Handler’s... method is used to display any form of progress in the user interface while the background computation is still executing For instance, it can be used to animate a progress bar or show logs in a text field onPostExecute(Result), invoked on the UI thread after the background computation finishes The result of the background computation is passed to this step as a parameter 30 DONG NAI UNIVERSITY OF... of obtainMessage(), allowing you to just create an empty Message object, or messages holding arguments Example // thread 1 produces some local data String // localData = “Greeting from thread 1”; thread 1 requests a message & adds localData to it Message mgs = myHandler.obtainMessage (1, localData); 16 DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Handler class  sendMessage Method You deliver the message ... launch additional Threads for specific purposes DONG NAI UNIVERSITY OF TECHNOLOGY 1.1 Introduction Multi- Threading DONG NAI UNIVERSITY OF TECHNOLOGY 1.1 Introduction Multi- Threading Threads... DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Handler class  Examples – using post 22 DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Handler class 23 DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Handler class 24 DONG NAI. .. in the EditText - Must use MultiThreading (Message or post) 25 DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 AsyncTask class 26 DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 AsyncTask class 27 DONG NAI UNIVERSITY

Ngày đăng: 04/12/2015, 02:15

Mục lục

  • Slide 1

  • Slide 2

  • Slide 3

  • Slide 4

  • Slide 5

  • Slide 6

  • Slide 7

  • Slide 8

  • Slide 9

  • Slide 10

  • Slide 11

  • Slide 12

  • Slide 13

  • Slide 14

  • Slide 15

  • Slide 16

  • Slide 17

  • Slide 18

  • Slide 19

  • Slide 20

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

Tài liệu liên quan