25
Android Programming Lecture 22: Dialog Boxes Phone Activity 11/30/2011

Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

Android Programming Lecture 22:

Dialog Boxes

Phone Activity

11/30/2011

Page 2: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

Generic Dialogs

dialog_b_layout.xml

Page 3: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

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

Page 4: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

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…

Page 5: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

Dialog Boxes: Dismiss and Cancel

Page 6: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

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);

Page 7: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

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)

Page 8: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

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

Page 9: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

DialogBoxes: AlertDialog

Page 10: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

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

Page 11: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

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)

Page 12: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

DialogBoxes: ProgressDialog

Note the onPrepareDialog/removeDialog issue

Page 13: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

DialogBoxes: ProgressDialog Examples

If I call showDialog(COUNTING_INDETERMINATE_DIALOG), I get this…

Page 14: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

DialogBoxes: ProgressDialog Examples

If I call showDialog(COUNTING_PROGESS_DIALOG), I get this…

Page 15: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

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

Page 16: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

DialogBoxes: Dialog-Themed Activities

Everything else with Activities stays the same, including normal Intent usage

Page 17: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

Phone Activity

Page 18: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

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

Page 19: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

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”>

Page 20: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

Phone & Network Information Example

Page 21: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

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

Page 22: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

Phone State Changes Example

This continued to be updated while I responded to the incoming call notice (ignoring the call).

Page 23: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

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

Page 24: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

Initiating A Call Example

Page 25: Android Programming Lecture 1 - Wake Forest Universitycsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture22.pdf · 2011-12-04 · Android Programming Lecture 22: Dialog Boxes Phone

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