Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
Android Programming Lecture 22:
Dialog Boxes
Phone Activity
11/30/2011
Generic Dialogs
dialog_b_layout.xml
Dialog Boxes: Dismiss and Cancel • dismiss() and cancel() methods can be called on dialog boxes to
programmatically close the boxes • Dialog state is preserved even after these methods are called so can
access information in dialog – They are effectively cached
• If call removeDialog(int dialogID), it is dismissed AND removed from cache
• Implement DialogInterface.onDismissListener
One method to implement: public void onDismiss(DialogInterface dialog) Hook to a dialog with setOnDismissListener
• Implement DialogInterface.onCancelListener One method: public void onCancel(DialogInterface dialog) Triggered by a cancel() call or hardware back button Triggered in addition to onDismiss Hook to a dialog with setOnCancelListener
Dialog Boxes: Dismiss and Cancel • There does not appear to be a simple way to get
an ID associated with a dialog: – Using a single onDismiss or onCancel method and
differentiating between dialogs inside that method won’t work without access to an ID to differentiate
– Employ anonymous inner class listeners associated with each dialog
• Leads to some repetitive code…
Dialog Boxes: Dismiss and Cancel
Dialog Boxes: AlertDialog • The AlertDialog class supports most standard
dialog functionality – Textual information presented to user – 1-3 Buttons for feedback – RadioButton and Checkbox entry – Text entry (really, any arbitrary view)
• Employ an AlertDialog.Builder to build the dialog
– AlertDialog.Builder builder = new AlertDialog.Builder(Context context);
Dialog Boxes: AlertDialog • AlertDialog.Builder commonly used methods:
– create(): retrieve the constructed dialog box so it can be shown
– setMessage(String): set message to display
– setPositiveButton(String label, DialogInterface.OnClickListener listener) OR setNeutralButton(String label, DialogInterface.OnClickListener listener) OR setNegativeButton(String label, DialogInterface.OnClickListener listener):
add 1 – 3 buttons – setCancelable(boolean):
allow or disallow “Back” button press – setItems(String[] itemsList, OnClickListener) OR setSingleChoiceItems(String[]
itemsList, int selectedPosition, OnClickListener) OR setMultiChoiceItems(String[] itemsList, boolean[] checkedItems, OnClickListener):
show list of items to select, show radiobutton list, show checkbox list with multiple selection capability – setView(View view):
set an arbitrary view as the content of the dialog box (such as an EditText)
Dialog Boxes: AlertDialog – setPositiveButton(String label,
DialogInterface.OnClickListener listener) OR setNeutralButton(String label, DialogInterface.OnClickListener listener) OR setNegativeButton(String label, DialogInterface.OnClickListener listener)
The DialogInterface.OnClickListener requires implementing onClick(DialogInterface dialog, int buttonSelected) Compare buttonSelected against DialogInterface.BUTTON_POSITIVE, DialogInterface.BUTTON_NEGATIVE, DialogInterface.BUTTON_NEUTRAL
DialogBoxes: AlertDialog
Dialog Boxes: ProgressDialog • A ProgressDialog can show a textual message (or
view) and a progress indicator • Progress indicator:
– Indeterminate: Circular spinner (style constant: ProgressDialog.STYLE_SPINNER)
– Fixed: Horizontal bar (style constant: ProgressDialog.STYLE_HORIZONTAL)
• Construct with a Context: – ProgressDialog pd = new ProgressDialog(Context
context); – Use a function setProgressStyle to choose style
• Default is STYLE_SPINNER
Dialog Boxes: ProgressDialog • Commonly used functions:
– setMessage(String) • When employing STYLE_HORIZONTAL
– setMax(int) – setProgress(int) – incrementProgressBy(int)
• These are useful if need to show status of two events: (such as buffering progress and playing progress of a media track) – setSecondaryProgress(int) – incrementSecondaryProgressBy(int)
DialogBoxes: ProgressDialog
Note the onPrepareDialog/removeDialog issue
DialogBoxes: ProgressDialog Examples
If I call showDialog(COUNTING_INDETERMINATE_DIALOG), I get this…
DialogBoxes: ProgressDialog Examples
If I call showDialog(COUNTING_PROGESS_DIALOG), I get this…
Dialog Boxes: Dialog-Themed Activities • Activities can be
themed to have different appearances
• Supports things such as removing title bar
• One such theme makes Activity look like a Dialog box
• Can set theme in AndroidManifest.xml
http://developer.android.com/ reference/android/R.style.html
DialogBoxes: Dialog-Themed Activities
Everything else with Activities stays the same, including normal Intent usage
Phone Activity
Dealing With the Phone Itself
• Three basic options: – 1) Get information about phone & network
• Essentially parallel functionality to support querying phone connectivity and data connectivity
– 2) Monitor phone and data state – 3) Initiate outgoing call
• Cannot directly swap out standard “in-call” phone screen if using actual built-in phone processes
Telephony Manager & Permissions
• Like most other hardware-based functionalty, we have to use a “Manager”
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
• Request permission to get info about phone and monitor phone state
<uses-permission android:name=“android.permission.READ_PHONE_STATE”>
Phone & Network Information Example
Listening to Changes in Phone State • Have to extend
PhoneStateListener class (not implements, extends making a subclass)
• Instead of making your class do this (which could be difficult if you want to extend another class, like Activity or MapActivity), use anonymous inner classes
• Add code to define any
responses you want to support for 9 updates you can receive
• Also have to request to TelephonyManager to listen for such events (and un-request when done)
See example on next page to see how to monitor for incoming phone calls and service changes
Phone State Changes Example
This continued to be updated while I responded to the incoming call notice (ignoring the call).
Initiating A Call • Exploit Intent.ACTION_CALL and
Intent.ACTION_DIAL sending phone number as URI for Intent
• ACTION_DIAL: Open the default dialer with number loaded
• ACTION_CALL: Do the dialing and call management automatically – Requires CALL_PHONE permission – Requires CALL_PRIVILEGED permission if want to
allow dialing emergency numbers
Initiating A Call Example
OverRiding Phone System (Outgoing)
• You could implement your own phone system (VOIP) and register it as an option for dialing on the phone
• Requires using Intent filters (we haven’t seen these) – Basically where you app advertises what it will
respond to