Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 61 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
61
Dung lượng
1,9 MB
Nội dung
DONG NAI UNIVERSITY OF TECHNOLOGY API Networking Multimedia APIs APIs DONG NAI UNIVERSITY OF TECHNOLOGY Networking APIs 1.1 Understanding Mobile Networking 1.2 Strict Mode with Networking 1.3 Accessing the Internet (HTTP) 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 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 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 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 ways to create Thread for networking, to see details please click the link below: http://android-developers.blogspot.com/2009/05/painless-threading.html DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Strict Mode with Networking - 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 DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Strict Mode with Networking - 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 DONG NAI UNIVERSITY OF TECHNOLOGY 1.2 Strict Mode with Networking - 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 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 DONG NAI UNIVERSITY OF TECHNOLOGY 1.3 Accessing the Internet (HTTP) Reading Data from the Web 10 DONG NAI UNIVERSITY OF TECHNOLOGY 2.3 Working with Video Cont 47 DONG NAI UNIVERSITY OF TECHNOLOGY 2.3 Working with Video Cont 48 DONG NAI UNIVERSITY OF TECHNOLOGY 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() Exercise: build a face detection production Examples: http://android-er.blogspot.com/2012/04/face-detection-for-camera.html http:// www.developer.com/ws/android/programming/face-detection-with-android-apis.htm l 49 DONG NAI UNIVERSITY OF TECHNOLOGY 2.4 Working with Audio Objectives: Recording Audio Playing Audio Sharing Audio Searching for Multimedia Working with Ringtones 50 DONG NAI UNIVERSITY OF TECHNOLOGY 2.4 Working with Audio Recording Audio The MediaRecorder object of the Android SDK provides audio recording functionality Here are steps: Instantiate a new MediaRecorder object Set the audio source Set the audio format to record with Set the file format to store the audio in Set the file to record to Prepare the object for recording Start the recording Stop and release the recording object when finished android.permission.RECORD_AUDIO 51 DONG NAI UNIVERSITY OF TECHNOLOGY 2.4 Working with Audio Cont 52 DONG NAI UNIVERSITY OF TECHNOLOGY 2.4 Working with Audio Cont 53 DONG NAI UNIVERSITY OF TECHNOLOGY 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: Instantiate a new MediaPlayer object Set the path to the file using the setDataSource() method Call the prepare() method of the MediaPlayer object Call the start() method to begin playback Playback can then be stopped with a call to the stop() method 54 DONG NAI UNIVERSITY OF TECHNOLOGY 2.4 Working with Audio Playing Audio 55 DONG NAI UNIVERSITY OF TECHNOLOGY 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 56 DONG NAI UNIVERSITY OF TECHNOLOGY 2.4 Working with Audio Sharing Audio 57 DONG NAI UNIVERSITY OF TECHNOLOGY 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 58 DONG NAI UNIVERSITY OF TECHNOLOGY 2.4 Working with Audio Searching for Multimedia 59 DONG NAI UNIVERSITY OF TECHNOLOGY 2.4 Working with Audio Working with Ringtones Manage ringtones through the RingtoneManager object RingtoneManager.setActualDefaultRingtoneUri ( getApplicationContext(), RingtoneManager.TYPE_RINGTONE, Uri.parse("file here") ); 60 DONG NAI UNIVERSITY OF TECHNOLOGY END 61 [...]... 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... 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... 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 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...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... TECHNOLOGY 2.2 Working with the Camera Cont… 30 DONG NAI UNIVERSITY OF TECHNOLOGY 2.2 Working with the Camera Cont… 31 DONG NAI UNIVERSITY OF TECHNOLOGY 2.2 Working with the Camera Cont… 32 DONG NAI UNIVERSITY OF TECHNOLOGY 2.2 Working with the Camera Cont… 33 DONG NAI UNIVERSITY OF TECHNOLOGY 2.2 Working with the Camera Cont… 34 DONG NAI UNIVERSITY OF TECHNOLOGY 2.2 Working with the Camera... assignment of a SurfaceHolder of an appropriate type 26 DONG NAI UNIVERSITY OF TECHNOLOGY 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): 1 Create a new class extending SurfaceView and implement SurfaceHolder.Callback For... 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 DONG NAI UNIVERSITY OF TECHNOLOGY 2.2 Working with the Camera Capturing... point, depth of field, infinity, …) White balance settings (fluorescent, incandescent, …) Scene modes (snow, beach, fireworks, …) Effects (photo negative, sepia, …) Anti-banding settings (noise reduction) 35 DONG NAI UNIVERSITY OF TECHNOLOGY 2.2 Working with the Camera Zooming the Camera The camera zoom setting is controlled using the startSmoothZoom() and stopSmoothZoom() methods of the... 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 27 DONG NAI UNIVERSITY OF TECHNOLOGY 2.2 Working with the Camera Capturing Still Images Using the Camera 6 Include some way, such as a button, for the user to trigger the capturing... surfaceDestroyed() method 28 DONG NAI UNIVERSITY OF TECHNOLOGY 2.2 Working with the Camera Capturing Still Images Using the Camera You must test on Physical Mobile Device 29 DONG NAI UNIVERSITY OF TECHNOLOGY ... 49 DONG NAI UNIVERSITY OF TECHNOLOGY 2.4 Working with Audio Objectives: Recording Audio Playing Audio Sharing Audio Searching for Multimedia Working with Ringtones 50 DONG NAI UNIVERSITY... Working with Audio Recording Audio The MediaRecorder object of the Android SDK provides audio recording functionality Here are steps: Instantiate a new MediaRecorder object Set the audio source... 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