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 informa
Trang 31.1 Working with Telephony Utilities
If your application uses telephony features, make sure you set the <uses-feature> 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:
http://developer.android.com/guide/topics/manifest/uses-feature-element.html#hw-features
Trang 41.1 Working with Telephony Utilities
Gaining Permission to Access Phone State Information
<uses-permission android:name=
"android.permission.READ_PHONE_STATE" />
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:
http://developer.android.com/reference/android/telephony/TelephonyManager.html
Trang 55
Trang 61.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
Trang 77
Trang 81.1 Working with Telephony Utilities Cont…
Trang 91.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.
Trang 101.1 Working with Telephony Utilities Cont…
Trang 111.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()
Trang 121.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
Trang 141.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
Trang 1515
Trang 161.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
Trang 17pdus
Trang 181.2 Using SMS By Manifest
Trang 191.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 );
Trang 201.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
Trang 211.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.
Trang 221.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);
<uses-permission android:name=
Trang 231.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
Trang 241.3 Making and Receiving Phone Calls
Receiving Phone Calls
Trang 251.3 Making and Receiving Phone Calls
Receiving Phone Calls
Trang 261.3 Making and Receiving Phone Calls
Exercise 1: Write an application to manage Calls log
http://developer.android.com/reference/android/provider/CallLog.Calls.html
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.
Trang 282.1 Using Global Positioning Services
<uses-feature android:name="android.hardware.location" />
<uses-feature android:name="android.hardware.location.gps" />
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
Trang 292.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
Trang 302.1 Using Global Positioning Services
Trang 3131 2.1 Using Global Positioning Services
Trang 322.1 Using Global Positioning Services
Trang 3333 2.1 Using Global Positioning Services
Trang 342.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<Address>
Trang 3535 2.2 Geocoding Locations
Trang 362.2 Geocoding Locations
Trang 3737 2.2 Geocoding Locations
Trang 382.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
Trang 3939 2.3 Mapping Locations
Mapping Intents
Trang 402.3 Mapping Locations
Mapping Intents
< uses-permission android:name ="android.permission.ACCESS_GPS" />
< uses-permission android:name ="android.permission.ACCESS_LOCATION" />
< uses-permission android:name ="android.permission.ACCESS_FINE_LOCATION" />
< uses-permission android:name ="android.permission.ACCESS_COARSE_LOCATION" />
< uses-permission android:name ="android.permission.INTERNET" />
< uses-feature android:name ="android.hardware.location" />
< uses-feature android:name ="android.hardware.location.gps" />
Trang 422.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:
Trang 432.3 Mapping Locations
Mapping Views
Getting Your Debug API Key:
1 Generate an MD5 fingerprint for your debug certificate
2 Sign in to http://code.google.com/android/maps-api-signup.html 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
Trang 442.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)
Exercise for Mapping Views:
Write an application to support 2 features:
- Zooming the MapView
- and Marking the spot
Trang 4545