intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Bài giảng Lập trình cho thiết bị di động: Chương 5 - ĐH Đồng Nai

Chia sẻ: Na Na | Ngày: | Loại File: PPTX | Số trang:61

121
lượt xem
12
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Bài giảng Lập trình cho thiết bị di động: Chương 5 - Networking APIs VÀ Multimedia APIs trình bày các nội dung về Networking APIs và Multimedia APIs. Tham khảo nội dung bài giảng để hiểu rõ hơn về các nội dung trên.

Chủ đề:
Lưu

Nội dung Text: Bài giảng Lập trình cho thiết bị di động: Chương 5 - ĐH Đồng Nai

  1. DONG NAI UNIVERSITY OF TECHNOLOGY API 1 2 Networking Multimedia APIs APIs 1
  2. DONG NAI UNIVERSITY OF TECHNOLOGY 1. Networking APIs 1.1 Understanding Mobile Networking 1.2 Strict Mode with Networking 1.3 Accessing the Internet (HTTP) 2
  3. DONG NAI UNIVERSITY OF TECHNOLOGY 1.1 Understanding Mobile Networking Ø Networking on the Android platform is standardized, using a combination of powerful yet familiar technologies and libraries such as java.net. Ø Network implementation is generally straightforward, but mobile application developers need to plan for less stable connectivity than one might expect in a home or office network setting—connectivity depends on the location of the users and their devices. Ø developer must take extra care when designing network- enabled applications 3
  4. DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Strict Mode with Networking Ø Strict mode is a method that developers can use to detect operations performed on the main thread that should not be there. Ø API Level 11 expanded upon strict mode in ways that impact networking code. By default, if you perform network operations on the main thread, your application throws an exception, specifically android.os.NetworkOnMainThreadException. Ø 2 ways to avoid this is to use proper coding techniques and put all networking operations on a thread other than the main thread (should use AsyncTask class). Ø Or call the permitAll() method to skip strict mode entirely 4
  5. DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Strict Mode with Networking Ø If you want to use networking in Main Thread, the coding: if (android.os.Build.VERSION.SDK_INT > 10) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .permitAll().build(); StrictMode.setThreadPolicy(policy); } This is not recommended for production applications. WHY? The next slide you will learn 3 ways to create Thread for networking, to see details please click the link below: http://android-developers.blogspot.com/2009/05/painless-threading.html 5
  6. DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Strict Mode with Networking Ø 1 - If not, you must write coding on a Thread other At first, this code seems to be a good solution to your problem, as it does not block the UI thread. Unfortunately, it violates the single thread model: the Android UI toolkit is not thread-safe and must always be manipulated on the UI thread. In this piece of code, the ImageView is manipulated on a worker thread, which can cause really weird problems. Tracking down and fixing such bugs can be difficult and time-consuming. 6
  7. DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Strict Mode with Networking Ø 2 - If not, you must write coding on a Thread other Unfortunately, these classes and methods also tend to make your code more complicated and more difficult to read. It becomes even worse when your implement complex operations that require frequent UI updates. 7
  8. DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Strict Mode with Networking Ø 3 - If not, you must write coding on a Thread other ü Android 1.5 offers a new utility class, called AsyncTask, that simplifies the creation of long-running tasks that need to communicate with the user interface. The goal of AsyncTask is to take care of thread management for you. 8
  9. DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 Accessing the Internet (HTTP) ü The most common way to transfer data to and from the network is to use HTTP. You can use HTTP to encapsulate almost any type of data and to secure the data with Secure Sockets Layer (SSL) Objectives: q Reading Data from the Web q Using HttpURLConnection q Displaying Images from a Network Resource q Retrieving Android Network Status 9
  10. DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 Accessing the Internet (HTTP) q Reading Data from the Web 10
  11. DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 Accessing the Internet (HTTP) q Reading Data from the Web 11
  12. DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 Accessing the Internet (HTTP) q Cont… 12
  13. DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 Accessing the Internet (HTTP) q Using HttpURLConnection This object to do a little reconnaissance on our URL before we transfer too much data. It retrieves some information about the resource referenced by the URL object, including HTTP status and header information. 13
  14. DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 Accessing the Internet (HTTP) q Displaying Images from a Network Resource 14
  15. DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 Accessing the Internet (HTTP) q Displaying Images from a Network Resource 15
  16. DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 Accessing the Internet (HTTP) q Displaying Images from a Network Resource 16
  17. DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 Accessing the Internet (HTTP) q Retrieving Android Network Status ü The Android SDK provides utilities for gathering information about the current state of the network.This is useful to determine whether a network connection is even available before trying to use a network resource. ü The ConnectivityManager class provides a number of methods to do this 17
  18. DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 Accessing the Internet (HTTP) q Retrieving Android Network Status Only physical device testing can truly reveal these results. 18
  19. DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 Accessing the Internet (HTTP) q Retrieving Android Network Status 19
  20. DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 Accessing the Internet (HTTP) q Retrieving Android Network Status 20
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2