Tải bản đầy đủ (.pptx) (61 trang)

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

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.49 MB, 61 trang )

DONG NAI UNIVERSITY OF TECHNOLOGY

API
1

2

Networking

Multimedia
APIs

APIs

1


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


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 networkenabled 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 = 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:
/>5


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


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


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


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:
 Reading Data from the Web
 Using HttpURLConnection
 Displaying Images from a Network Resource
 Retrieving Android Network Status

9


DONG NAI UNIVERSITY OF TECHNOLOGY

1.3 Accessing the Internet (HTTP)
 Reading Data from the Web

android:name="android.permission.
INTERNET"/>

10


DONG NAI UNIVERSITY OF TECHNOLOGY

1.3 Accessing the Internet (HTTP)
 Reading Data from the Web


11


DONG NAI UNIVERSITY OF TECHNOLOGY

1.3 Accessing the Internet (HTTP)

 Cont…

12


DONG NAI UNIVERSITY OF TECHNOLOGY

1.3 Accessing the Internet (HTTP)
 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


DONG NAI UNIVERSITY OF TECHNOLOGY

1.3 Accessing the Internet (HTTP)
 Displaying Images from a Network Resource


android:name="android.permission.
INTERNET"/>

14


DONG NAI UNIVERSITY OF TECHNOLOGY

1.3 Accessing the Internet (HTTP)
 Displaying Images from a Network Resource

15


DONG NAI UNIVERSITY OF TECHNOLOGY

1.3 Accessing the Internet (HTTP)
 Displaying Images from a Network Resource

16


DONG NAI UNIVERSITY OF TECHNOLOGY

1.3 Accessing the Internet (HTTP)
 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


DONG NAI UNIVERSITY OF TECHNOLOGY

1.3 Accessing the Internet (HTTP)
 Retrieving Android Network Status

Only physical device testing can truly
reveal these results.

<uses-permission android:name="android.permission.INTERNET"/>
android:name="android.permission.ACCESS_NETWORK_STATE"/>
18


DONG NAI UNIVERSITY OF TECHNOLOGY

1.3 Accessing the Internet (HTTP)
 Retrieving Android Network Status

19


DONG NAI UNIVERSITY OF TECHNOLOGY


1.3 Accessing the Internet (HTTP)
 Retrieving Android Network Status

20


DONG NAI UNIVERSITY OF TECHNOLOGY

1.3 Accessing the Internet (HTTP)
 Retrieving Android Network Status

21


DONG NAI UNIVERSITY OF TECHNOLOGY

2. Multimedia APIs
2.1 Working with Multimedia
2.2 Working with the Camera
2.3 Working with Video
2.4 Working with Audio

22


DONG NAI UNIVERSITY OF TECHNOLOGY

2.1 Working with Multimedia
The Android SDK provides a variety of methods for

applications to incorporate audio and visual media, including
support for many different media types and formats.
The multimedia features of the Android platform generally
fall into three categories:
 Still images (recorded with the camera)
 Audio (recorded with the microphone, played back with
speakers or audio output)
 Video (recorded with the camera and microphone, played
back with speakers or video output)
<uses-feature android:name="android.hardware.microphone" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
23


DONG NAI UNIVERSITY OF TECHNOLOGY

2.2 Working with the Camera
Many Android devices have at least one camera for
capturing images and video. If the user’s device has builtin camera hardware, the user can capture still images
using the Camera object (android.hardware.Camera) of
the Android SDK.
 Beginning in Android 4.0 (API Level 14), the system
broadcasts when a new picture or video has been taken
with the camera. Your applications can listen for these
events and react to them.

24



DONG NAI UNIVERSITY OF TECHNOLOGY

2.2 Working with the Camera
Objectives:
 Capturing Still Images Using the Camera
 Configuring Camera Mode Settings
 Working with Common Camera Parameters
 Zooming the Camera
 Sharing Images
 Assigning Images as Wallpapers

25


×