Bài giảng Lập trình cho thiết bị di động - Chương 6: Telephony APIs và location base services APIs - Trần Duy Thanh

DONG NAI UNIVERSITY OF TECHNOLOGY  
API  
2
1
LBS  
Telephony  
APIs  
APIs  
1
DONG NAI UNIVERSITY OF TECHNOLOGY  
1. Telephony APIs  
1.1 Working with Telephony Utilities  
1.2 Using SMS  
1.3 Making and Receiving Phone Calls  
2
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.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:  
3
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.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:  
4
DONG NAI UNIVERSITY OF TECHNOLOGY  
➢ Cont…  
1.1 Working with Telephony Utilities  
5
DONG NAI UNIVERSITY OF TECHNOLOGY  
➢ Cont…  
1.1 Working with Telephony Utilities  
➢ 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.  
6
DONG NAI UNIVERSITY OF TECHNOLOGY  
➢ Cont…  
1.1 Working with Telephony Utilities  
7
DONG NAI UNIVERSITY OF TECHNOLOGY  
➢ Cont…  
1.1 Working with Telephony Utilities  
8
DONG NAI UNIVERSITY OF TECHNOLOGY  
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.  
9
DONG NAI UNIVERSITY OF TECHNOLOGY  
➢ Cont…  
1.1 Working with Telephony Utilities  
10  
DONG NAI UNIVERSITY OF TECHNOLOGY  
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()  
11  
DONG NAI UNIVERSITY OF TECHNOLOGY  
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.  
12  
DONG NAI UNIVERSITY OF TECHNOLOGY  
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  
<uses-permission android:name=  
"android.permission.SEND_SMS" />  
<uses-permission android:name=  
"android.permission.RECEIVE_SMS" />  
13  
DONG NAI UNIVERSITY OF TECHNOLOGY  
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.  
14  
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.2 Using SMS  
➢ Cont..  
15  
DONG NAI UNIVERSITY OF TECHNOLOGY  
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  
16  
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.2 Using SMS  
➢ By coding  
pdus  
17  
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.2 Using SMS  
➢ By Manifest  
18  
DONG NAI UNIVERSITY OF TECHNOLOGY  
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)  
19  
DONG NAI UNIVERSITY OF TECHNOLOGY  
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  
20  
Tải về để xem bản đầy đủ
pptx 45 trang baolam 32700 Free
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Lập trình cho thiết bị di động - Chương 6: Telephony APIs và location base services APIs - Trần Duy Thanh", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pptxbai_giang_lap_trinh_cho_thiet_bi_di_dong_chuong_6_telephony.pptx