Bài giảng Lập trình cho thiết bị di động - Bài 5: Networking APIs và Multimedia APIs
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.
r code more complicated and more difficult to read. It becomes even worse when your implement complex operations that require frequent UI updates. 8 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. 9 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) Reading Data from the Web Using HttpURLConnection Displaying Images from a Network Resource Retrieving Android Network Status Objectives: 10 1.3 Accessing the Internet (HTTP) Reading Data from the Web 11 1.3 Accessing the Internet (HTTP) Reading Data from the Web 12 1.3 Accessing the Internet (HTTP) Cont… 13 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. 14 1.3 Accessing the Internet (HTTP) Displaying Images from a Network Resource 15 1.3 Accessing the Internet (HTTP) Displaying Images from a Network Resource 16 1.3 Accessing the Internet (HTTP) Displaying Images from a Network Resource 17 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 18 1.3 Accessing the Internet (HTTP) Retrieving Android Network Status Only physical device testing can truly reveal these results. 19 1.3 Accessing the Internet (HTTP) Retrieving Android Network Status 20 1.3 Accessing the Internet (HTTP) Retrieving Android Network Status 21 1.3 Accessing the Internet (HTTP) Retrieving Android Network Status 22 2. Multimedia APIs 2.1 Working with Multimedia 2.2 Working with the Camera 2.3 Working with Video 2.4 Working with Audio 23 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) 24 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 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. 25 2.2 Working with the Camera 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: 26 2.2 Working with the Camera 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. 27 2.2 Working with the Camera 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): Create a new class extending SurfaceView and implement SurfaceHolder.Callback. For this example, we name this class CameraSurfaceView. In the surfaceCreated() method, get an instance of the Camera object. In the surfaceChanged() method, configure and apply the Camera.Parameters; then call the startPreview() method. Add a method in CameraSurfaceView for capturing images. Add the CameraSurfaceView to an appropriate layout. 28 2.2 Working with the Camera Capturing Still Images Using the Camera Include some way, such as a button, for the user to trigger the capturing of images. Implement a PictureCallback class to handle the storing of the captured image. Add the android.permission.CAMERA permission to the AndroidManifest.xml file. Release the Camera object in the surfaceDestroyed() method. 29 2.2 Working with the Camera Capturing Still Images Using the Camera You must test on Physical Mobile Device 30 2.2 Working with the Camera Cont… 31 2.2 Working with the Camera Cont… 32 2.2 Working with the Camera Cont… 33 2.2 Working with the Camera Cont… 34 2.2 Working with the Camera Cont… 35 2.2 Working with the Camera 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) 36 2.2 Working with the Camera 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, and 10x) getZoomRatios() 37 2.2 Working with the Camera Zooming the Camera 38 2.2 Working with the Camera Sharing Images 39 2.2 Working with the Camera Assigning Images as Wallpapers Add more permission 40 2.3 Working with Video 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. Objectives: Recording Video Playing Video Working with face detection 41 2.3 Working with Video 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. 42 2.3 Working with Video Recording Video You must test on Physical Mobile Device 43 2.3 Working with Video Cont… 44 2.3 Working with Video Cont… 45 2.3 Working with Video Cont… 46 2.3 Working with Video Playing Video Use the MediaController class, VideoView widget Use the Intent Read more MediaController: Read more VideoView control: 47 2.3 Working with Video Cont 48 2.3 Working with Video Cont 49 2.3 Working with Video Working with face detection Beginning in Android 4.0 (API Level 14), the Camera class supports face detection Here are steps: Register a Camera.FaceDetectionListener Start the Camera and call the startFaceDetection() method Get an onFaceDetection() callback event, which returns an array of Camera.Face objects you can inspect When you’re done, call stopFaceDetection(). Examples: Exercise: build a face detection production 50 2.4 Working with Audio Objectives: Recording Audio Playing Audio Sharing Audio Searching for Multimedia Working with Ringtones 51 2.4 Working with Audio 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 52 2.4 Working with Audio Cont 53 2.4 Working with Audio Cont 54 2.4 Working with Audio 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. 55 2.4 Working with Audio Playing Audio 56 2.4 Working with Audio Sharing Audio Audio can be shared with the rest of the system.The ContentResolver can send the file to the MediaStore content provider. 57 2.4 Working with Audio Sharing Audio 58 2.4 Working with Audio 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. 59 2.4 Working with Audio Searching for Multimedia 60 2.4 Working with Audio Working with Ringtones Manage ringtones through the RingtoneManager object RingtoneManager.setActualDefaultRingtoneUri ( getApplicationContext(), RingtoneManager.TYPE_RINGTONE, Uri.parse("file here") ); END 61
File đính kèm:
- 5 - NETWORKING APIs VÀ MULTIMEDIA APIs.pptx