Bài giảng Lập trình cho thiết bị di động: Chương 5 - ĐH Đồng Nai
Thông tin tài liệu:
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 5 - ĐH Đồng Nai DONG NAI UNIVERSITY OF TECHNOLOGY API 1 2Networking Multimedia APIs APIs 1 DONG NAI UNIVERSITY OF TECHNOLOGY1. Networking APIs 1.1 Understanding Mobile Networking 1.2 Strict Mode with Networking 1.3 Accessing the Internet (HTTP) 2 DONG NAI UNIVERSITY OF TECHNOLOGY1.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 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 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 = newStrictMode.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, tosee details please click the link below:http://android-developers.blogspot.com/2009/05/painless-threading.html 5 DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Strict Mode with Networking Ø 1 - If not, you must write coding on a Thread otherAt first, this code seems to be a good solution to your problem,as it does not block the UI thread. Unfortunately, it violates thesingle thread model: the Android UI toolkit is not thread-safeand must always be manipulated on the UI thread. In thispiece of code, the ImageView is manipulated on a workerthread, which can cause really weird problems. Tracking downand fixing such bugs can be difficult and time-consuming. 6 DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Strict Mode with Networking Ø 2 - If not, you must write coding on a Thread otherUnfortunately, these classes and methods also tend to makeyour code more complicated and more difficult to read. Itbecomes even worse when your implement complexoperations that require frequent UI updates. 7 DONG NAI UNIVERSITY OF TECHNOLOGY1.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 DONG NAI UNIVERSITY OF TECHNOLOGY1.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 ...
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 tinGợi ý tài liệu 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 343 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 266 0 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 208 0 0 -
Giới thiệu môn học Ngôn ngữ lập trình C++
5 trang 195 0 0 -
43 trang 189 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 168 0 0 -
72 trang 167 0 0
-
Luận văn: Nghiên cứu kỹ thuật giấu tin trong ảnh Gif
33 trang 153 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 148 0 0 -
33 trang 145 0 0
-
Giáo trình Lập trình Android cơ bản: Phần 1
190 trang 135 0 0 -
Báo cáo thực tập Công nghệ thông tin: Lập trình game trên Unity
27 trang 118 0 0 -
Giáo trình về phân tích thiết kế hệ thống thông tin
113 trang 114 0 0 -
LUẬN VĂN: Tìm hiểu kỹ thuật tạo bóng cứng trong đồ họa 3D
41 trang 109 0 0 -
Bài giảng Kỹ thuật lập trình - Chương 10: Tổng kết môn học (Trường Đại học Bách khoa Hà Nội)
67 trang 106 0 0 -
Giáo trình Nhập môn lập trình VB6: Phần 2
184 trang 93 0 0 -
17 trang 93 0 0
-
10 trang 91 0 0
-
Giáo trình Nhập môn lập trình VB6: Phần 1
246 trang 85 0 0 -
Giáo trình toán rời rạc - Phụ lục 2
15 trang 85 0 0