Bài giảng Lập trình cho thiết bị di động - Bài 6: Telephony APIs và Location Base Services APIs
1.1 Working with Telephony Utilities
1.2 Using SMS
1.3 Making and Receiving Phone Calls
Tóm tắt nội dung Bài giảng Lập trình cho thiết bị di động - Bài 6: Telephony APIs và Location Base Services APIs, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 05/09/2013 ‹#› 1 API 1 Telephony APIs 2 LBS APIs 2 1. Telephony APIs 1.1 Working with Telephony Utilities 1.2 Using SMS 1.3 Making and Receiving Phone Calls 3 1.1 Working with Telephony Utilities If your application uses telephony features, make sure you set the tag with the android.hardware.telephony feature (or one of its sub-features) in your application’s manifest file to ensure your application is installed only on compatible devices. Read more Hardware Features: 4 1.1 Working with Telephony Utilities Gaining Permission to Access Phone State Information You can use the TelephonyManager object to retrieve the state of the phone and some information about the phone service itself, such as the phone number of the handset. Read more details: 5 1.1 Working with Telephony Utilities Cont… 6 1.1 Working with Telephony Utilities Cont… Listening for changes in the call state can enable an application to react appropriately to something the user might be doing. For instance, a game might automatically pause and save state information when the phone rings so that the user can safely answer the call. An application can register to listen for changes in the call state by making a call to the listen() method of TelephonyManager. 7 1.1 Working with Telephony Utilities Cont… 8 1.1 Working with Telephony Utilities Cont… 9 1.1 Working with Telephony Utilities Get telephony service Add the PhoneStateListener.LISTEN_SERVICE_STATE flag to the listener described earlier and implement the onServiceStateChanged method, which receives an instance of the ServiceState object. 10 1.1 Working with Telephony Utilities Cont… 11 1.1 Working with Telephony Utilities Working with Phone Numbers The resulting output to the log would be the string “999-555-1212” Check the phone number is an emergency phone number by calling PhoneNumberUtils.isEmergencyNumber() 12 1.1 Working with Telephony Utilities Working with Phone Numbers The formatNumber() method can also take an Editable as a parameter to format a number in place.The useful feature here is that you can assign the PhoneNumberFormattingTextWatcher object to watch a TextView (or EditText for user input) and format phone numbers as they are entered. 13 1.2 Using SMS Gaining Permission to Send and Receive SMS Messages SMS functionality requires two different permissions, depending on whether the application sends or receives messages 14 1.2 Using SMS Sending an SMS To send an SMS, an application first needs to get an instance of the SmsManager. final SmsManager sms = SmsManager.getDefault(); Now use sendTextMessage method: sms.sendTextMessage( "0987773061", null, "Hello!", null, null); BUT the application does not know whether the actual sending of the SMS was successful without providing a PendingIntent to receive the broadcast of this information. 15 1.2 Using SMS Cont.. 16 1.2 Using SMS Receiving an SMS The application must register a BroadcastReceiver to listen for the Intent action associated with receiving an SMS (I made 2 examples in the chapter 4 – Multithreading & Service) Two ways to register a BroadcastReceiver In coding : using the registerReceiver method In Manifest XML: using the receiver tag 17 1.2 Using SMS By coding pdus 18 1.2 Using SMS By Manifest 19 1.2 Using SMS Reading SMS from Inbox All available column names in SMS table: [ _id, thread_id, address, person, date, protocol, read, status, type, reply_path_present, subject, body, service_center, locked, error_code, seen] Using the ContentResolver to query SMS : ContentResolver contentResolver = getContentResolver(); Cursor cursor = contentResolver.query( Uri.parse( "content://sms/inbox" ), null, null, null, null); // Process cursor here (using available column names in SMS table) 20 1.3 Making and Receiving Phone Calls You can also use two emulator instances to test calling to another handset. As with the SMS sending, the port number of the emulator is the phone number that can be called. 2 emulator 21 1.3 Making and Receiving Phone Calls Making Phone Calls The Android SDK enables phone numbers to be passed to the dialer in two different ways: The first way is to launch the dialer with a phone number already entered.The user then needs to press the Send button to actually initiate the call.This method does not require any specific permissions. The second way is to actually place the call. This method requires the android.permission.CALL_PHONE permission to be added to the application’s AndroidManifest.xml file. 22 1.3 Making and Receiving Phone Calls Making Phone Calls Uri number = Uri.parse("tel:01656152042"); Intent dial = new Intent(Intent.ACTION_DIAL, number); startActivity(dial); Uri number = Uri.parse("tel:01656152042"); Intent call = new Intent(Intent. ACTION_CALL, number); startActivity(call); 23 1.3 Making and Receiving Phone Calls Receiving Phone Calls An application can register to answer incoming phone calls. To enable this in an application, you must implement a broadcast receiver to process intents with the action Intent.ACTION_ANSWER. You can use the CallLog.calls class to determine recent call information, such as Who called When they called Whether it was an incoming or outgoing call Whether or not anyone answered The duration of the call 24 1.3 Making and Receiving Phone Calls Receiving Phone Calls 25 1.3 Making and Receiving Phone Calls Receiving Phone Calls Modified Manifest XML 26 1.3 Making and Receiving Phone Calls Exercise 1: Write an application to manage Calls log Exercise 2: Write an application to manage Blacklist (has GUI) it is not possible to block incoming calls, if your application has no root privileges. please search on the Internet how to root android device 27 2. LBS APIs 2.1 Using Global Positioning Services 2.2 Geocoding Locations 2.3 Mapping Locations 28 2.1 Using Global Positioning Services Steps to determine device location: 1. Retrieve an instance of the LocationManager using a call to the getSystemService() method using the LOCATION_SERVICE. 2. Add an appropriate permission to the AndroidManifest.xml file, depending on what type of location information the application needs. 3. Choose a provider using either the getAllProviders() method or the getBestProvider() method. 4. Implement a LocationListener class. 5. Call the requestLocationUpdates() method with the chosen provider and the LocationListener object to start receiving location information. 29 2.1 Using Global Positioning Services Click here to download KML for emulator: https://sites.google.com/site/mengyangc/blog/akmlsampleandroidkmlfileformockgpslocationtestonemulator https://developers.google.com/kml/documentation/mapsSupport 30 2.1 Using Global Positioning Services Edit Manifest XML 31 2.1 Using Global Positioning Services 32 2.1 Using Global Positioning Services 33 2.1 Using Global Positioning Services 34 2.2 Geocoding Locations According to the Android documentation, AVDs that target the Google APIs enable developers to test on emulator instances with the “Google experience.” The Google APIs provide the capability to use Google Maps as well as a backend geocoder service. The Geocoder object can be used without any special permissions, some methods: getFromLocation getFromLocationName return List doGeoCodingFromLocationName("ĐH Công Nghiệp"); doGeoCodingFromLocation(); 35 2.2 Geocoding Locations 36 2.2 Geocoding Locations 37 2.2 Geocoding Locations 38 2.3 Mapping Locations The Android SDK provides two different methods to show a location with Google Maps: The first method is to use a location Uri to launch the built-in Google Maps application with the specified location. The second method is to use a MapView embedded within your application to display the map location. 39 2.3 Mapping Locations Mapping Intents 40 2.3 Mapping Locations Mapping Intents 41 2.3 Mapping Locations Mapping Views 42 2.3 Mapping Locations Mapping Views Getting Your Debug API Key: To use a MapView in your applications, you must obtain a Google Maps API Key from Google.The key is generated from an MD5 fingerprint of a certificate that you use to sign your applications. You need to do the following to generate the appropriate API key: 43 2.3 Mapping Locations Mapping Views Getting Your Debug API Key: 1. Generate an MD5 fingerprint for your debug certificate. 2. Sign in to with a Google account. 3. Accept the Terms of Service. 4. Paste in the fingerprint from Step 1. 5. Save the Android Maps API key presented on the next screen. Reading more: https://developers.google.com/maps/documentation/android/start#installing_the_google_maps_android_v2_api 44 2.3 Mapping Locations Mapping Views Some features in Mapview: Zooming the Map View Marking the Spot Hint: Reading… Page 206 – Android Wireless development (android 4.0) Using Location-Based Services APIs Or research on the internet Exercise for Mapping Views: Write an application to support 2 features: - Zooming the MapView - and Marking the spot END 45
File đính kèm:
- 6 - TELEPHONY APIs VÀ LOCATION BASE SERVICES APIs.pptx