Tải bản đầy đủ (.pptx) (45 trang)

6 - telephony apis và location base services apis

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.24 MB, 45 trang )















 




 !! !"#!!!"#$%&"'(&%)
%*'+*%'*,%'& !$ !%& !'!(# )!
!#%*
%*-'%'*,%'%&"'.
+, **#-!--# -!& !&#*#.& !

/


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.
%*-'*&%+.
+, **#- ---*#

0


Cont…

1


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.

2


Cont…

3



Cont…

4


/
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.

5


0




1!#%
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()




1!#%
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.




Gaining Permission to 
<uses-permission android:name=
"android.permission.SEND_SMS" />
<uses-permission android:name=
"android.permission.RECEIVE_SMS" />
SMS functionality requires two different permissions, depending on whether the application sends or
receives messages

/


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.

0


Cont


1


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 ')+&'6+7'#

In Manifest XML: using the receiver tag

2


By coding
!

3


By Manifest

4


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)

5
 
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.
#!


 

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.


 

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=
"android.permission.CALL_PHONE"/>


 

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

/
 

Receiving Phone Calls

0
 

Receiving Phone Calls
<uses-permission android:name=
"android.permission.READ_PHONE_STATE" />
<receiver android:name=
"tranduythanh.com.CallReceiver" >
<intent-filter>
<action android:name=
"android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
) 23

×