1. Trang chủ
  2. » Công Nghệ Thông Tin

5 - networking apis và multimedia apis

61 259 0
Tài liệu đã được kiểm tra trùng lặp

Đ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

Thông tin cơ bản

Định dạng
Số trang 61
Dung lượng 1,84 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

You can use HTTP to encapsulate almost any type of data and to secure the data with Secure Sockets Layer SSL Reading Data from the Web Using HttpURLConnection Displaying Images from a

Trang 2

1.1 Understanding Mobile Networking

1.2 Strict Mode with Networking

1.3 Accessing the Internet (HTTP)

Trang 3

 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

Trang 4

 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

Trang 5

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

Trang 6

 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

Trang 7

 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

Trang 8

 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

Trang 9

 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)

Reading Data from the Web

Using HttpURLConnection

Displaying Images from a Network Resource

Retrieving Android Network Status

Objectives:

Trang 10

Reading Data from the Web

<uses-permission

android:name="android.permission.

INTERNET" />

Trang 11

Reading Data from the Web

Trang 13

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

Trang 14

Displaying Images from a Network Resource

<uses-permission

android:name="android.permission.

INTERNET" />

Trang 15

Displaying Images from a Network Resource

Trang 16

Displaying Images from a Network Resource

Trang 17

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

Trang 18

Retrieving Android Network Status

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

< uses-permission

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

Only physical device testing can truly

reveal these results.

Trang 19

Retrieving Android Network Status

Trang 20

Retrieving Android Network Status

Trang 21

Retrieving Android Network Status

Trang 22

2.1 Working with Multimedia

2.2 Working with the Camera

2.3 Working with Video

2.4 Working with Audio

Trang 23

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" />

Trang 24

Many Android devices have at least one camera for

capturing images and video If the user’s device has

built-in 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.

Trang 25

Capturing Still Images Using the Camera

 Configuring Camera Mode Settings

 Working with Common Camera Parameters

 Zooming the Camera

 Sharing Images

 Assigning Images as Wallpapers

Objectives:

Trang 26

Capturing Still Images Using the Camera

 The Camera object controls the camera on devices that

have camera support enabled

 The preview feature of the camera relies on the

assignment of a SurfaceHolder of an appropriate type.

Trang 27

Capturing Still Images Using the Camera

 Follow these steps to add camera capture capability to

an application without having to draw preview frames

(the CameraSurfaceView displays the camera view):

1 Create a new class extending SurfaceView and

implement SurfaceHolder.Callback For this example,

we name this class CameraSurfaceView.

2 In the surfaceCreated() method, get an instance of the

Camera object

3 In the surfaceChanged() method, configure and apply the

Camera.Parameters; then call the startPreview() method.

4 Add a method in CameraSurfaceView for capturing images.

5 Add the CameraSurfaceView to an appropriate layout.

Trang 28

Capturing Still Images Using the Camera

6 Include some way, such as a button, for the user to trigger

the capturing of images

7 Implement a PictureCallback class to handle the storing of

the captured image

8 Add the android.permission.CAMERA permission to the

AndroidManifest.xml file

9 Release the Camera object in the surfaceDestroyed()

method

Trang 31

31

Trang 33

33

Trang 35

 Working with Common Camera Parameters

 You can use the Camera class to configure the

specific capture settings for a picture Many of the capture settings are stored in the

Camera.Parameters class, and set in the Camera

class using the setParameters() method

 Camera parameters

 Flash modes (where flash hardware is available)

 Focus types (fixed point, depth of field, infinity, …)

 White balance settings (fluorescent, incandescent, …)

 Scene modes (snow, beach, fireworks, …)

 Effects (photo negative, sepia, …)

 Anti-banding settings (noise reduction)

Trang 36

 Zooming the Camera

 The camera zoom setting is controlled using the

startSmoothZoom() and stopSmoothZoom() methods

of the Camera class

 Use Camera.Parameters class to set Zoom:

• Determining whether zooming is supported

isZoomSupported()

• Determining whether smooth zooming is supported

isSmoothZoomSupported()

• Determining the maximum zoom getMaxZoom()

• Retrieving the current zoom value getZoom()

• Setting the current zoom value setZoom()

• Calculating the zoom increments (for example, 1x, 2x,

Trang 37

 Zooming the Camera

Trang 40

 Video has become commonplace on devices Most

devices on the market now can record and play back

video, and this is no different with Android, although the

specific video features might vary from device to device

Trang 41

 Recording Video

Android applications can record video using the MediaRecorder

class Here are steps:

1 Instantiate a new MediaRecorder object

2 Set the video source

3 Set the video output format

4 Set the video size to record (optional)

5 Set the video frame rate (optional)

6 Set the video encoder

7 Set the file to record to (The extension must match output format.)

8 Set the preview surface

9 Prepare the object for recording

10 Start the recording

11 Stop and release the recording object when finished

Trang 43

43

Trang 45

45

Trang 46

 Playing Video

 Use the MediaController class, VideoView widget

 Use the Intent

Read more MediaController:

Trang 47

47

Trang 49

 Working with face detection

Beginning in Android 4.0 (API Level 14), the Camera

class supports face detection

Here are steps:

1 Register a Camera.FaceDetectionListener

2 Start the Camera and call the startFaceDetection() method

3 Get an onFaceDetection() callback event, which returns an

array of Camera.Face objects you can inspect

4 When you’re done, call stopFaceDetection().

Trang 50

 Recording Audio

 Playing Audio

 Sharing Audio

 Searching for Multimedia

 Working with Ringtones

Trang 51

 Recording Audio

The MediaRecorder object of the Android SDK provides

audio recording functionality

Here are steps:

1 Instantiate a new MediaRecorder object.

2 Set the audio source

3 Set the audio format to record with

4 Set the file format to store the audio in

5 Set the file to record to

6 Prepare the object for recording

7 Start the recording

8 Stop and release the recording object when finished

android.permission.RECORD_AUDIO

Trang 53

53

Trang 54

 Playing Audio

The MediaPlayer object can be used to play audio The

following steps are required to prepare a file for playback:

1 Instantiate a new MediaPlayer object.

2 Set the path to the file using the setDataSource() method.

3 Call the prepare() method of the MediaPlayer object.

4 Call the start() method to begin playback.

5 Playback can then be stopped with a call to the stop()

method

Trang 55

 Playing Audio

Trang 56

 Sharing Audio

Audio can be shared with the rest of the system.The

ContentResolver can send the file to the MediaStore

content provider

Trang 57

 Sharing Audio

Trang 58

 Searching for Multimedia

 You can use the search intent called

android.intent.action.MEDIA_SEARCH to search for

multimedia on a given device

 Also register an intent filter with your application to show

up as a source for multimedia with this action

Trang 59

 Searching for Multimedia

Trang 60

 Working with Ringtones

Manage ringtones through the RingtoneManager object

Trang 61

61

Ngày đăng: 06/07/2014, 18:35

TỪ KHÓA LIÊN QUAN

w