cosc 4730 android dialogs and notifications. notifications there are a couple of ways to notify...

34
Cosc 4730 Android Dialogs and notifications

Post on 21-Dec-2015

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Cosc 4730

AndroidDialogs and notifications

Page 2: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Notifications

• There are a couple of ways to notify users without interrupting what they are doing

• The first is Toast, use the factory method– Toast.makeText(getBaseContext(), "Text to display

to user", Toast.LENGTH_SHORT).show();– getBaseContext() or getContext()

• Toast.LENGTH_SHORT or Toast.LENGTH_LONG is the duration it will be displayed.

Page 3: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Status Bar Notifications• A status bar notification adds an icon

to the system's status bar (with an optional ticker-text message) and an expanded message in the "Notifications" window. – You can also configure the notification

to alert the user with a sound, a vibration, and flashing lights on the device.

• When the user selects the expanded message, Android fires an Intent that is defined by the notification (usually to launch an Activity).

Page 4: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Using notifications

• First get the NotificationManager– retrieve a reference to the NotificationManager

with getSystemService() • and then, when you want to notify the user,

pass it your Notification object with notify(). – Notification requires an icon, text, and a time

when to notify the user.

Page 5: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Example code

NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

int icon = R.drawable.icon;CharSequence tickerText = "Hello";long when = System.currentTimeMillis();Notification notification = new Notification(icon, tickerText,

when);

Page 6: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Using notifications (2)

• Notification's expanded message and Intent– The expanded message is when they pull down

the notification to look at it.– Intent• When they click on the icon, the activity that is to be

launched.

Page 7: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Example code

Context context = getApplicationContext();CharSequence contentTitle = "My notification";CharSequence contentText = "Hello World!";Intent notificationIntent = new Intent(this, <Activity name>.class);PendingIntent contentIntent = PendingIntent.getActivity(this, 0,

notificationIntent, 0);notification.setLatestEventInfo(context, contentTitle, contentText,

contentIntent);

Page 8: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Using notifications (3)

• And finally, use notify to have the system notify the user– You need a integer ID number as well.

mNotificationManager.notify(YOURAPP_NOTIFICATION_ID, notification);

Page 9: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Canceling notifications

• To cancel a specific notificationmNotificationManager.cancel(YOURAPP_NOTIFI

CATION_ID);• To cancel all of the notificationsmNotificationManager.cancelAll();

Page 10: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Adding to the notification

• To add sound– default sound

• notification.defaults |= Notification.DEFAULT_SOUND;

– To use a different sound with your notifications, pass a Uri reference to the sound field. The following example uses a known audio file saved to the device SD card:• notification.sound =

Uri.parse("file:///sdcard/notification/ringer.mp3");

– the audio file is chosen from the internal MediaStore's ContentProvider:• notification.sound =

Uri.withAppendedPath( Audio.Media.INTERNAL_CONTENT_URI, "6");

Page 11: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Adding to the notification (2)

• Adding vibration– To use the default pattern• notification.defaults |= Notification.DEFAULT_VIBRATE;

– To define your own vibration pattern, pass an array of long values to the vibrate field:• long[] vibrate = {0,100,200,300};• notification.vibrate = vibrate;

– You'll need to add <uses-permission android:name="android.permission.VIBRATE"></uses-permission> in the AndroidManifest.xml

Page 12: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Adding to the notification (3)

• Adding flashing lights– To use the default light setting, add "DEFAULT_LIGHTS" to the

defaults field:• notification.defaults |= Notification.DEFAULT_LIGHTS;

– To define your own color and pattern, define a value for the ledARGB field (for the color), the ledOffMS field (length of time, in milliseconds, to keep the light off), the ledOnMS (length of time, in milliseconds, to keep the light on), and also add "FLAG_SHOW_LIGHTS" to the flags field:• notification.ledARGB = 0xff00ff00;• notification.ledOnMS = 300;• notification.ledOffMS = 1000;• notification.flags |= Notification.FLAG_SHOW_LIGHTS;

Page 13: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Adding to the notification (4)• FLAG_AUTO_CANCEL

– flag Add this to the flags field to automatically cancel the notification after it is selected from the Notifications window.

• FLAG_INSISTENT– flag Add this to the flags field to repeat the audio until the user responds. – Should be flag "uh pick me! PICK ME!"

• FLAG_ONGOING_EVENT– flag Add this to the flags field to group the notification under the "Ongoing" title

in the Notifications window. – This indicates that the application is on-going — its processes is still running in

the background, even when the application is not visible (such as with music or a phone call).

• FLAG_NO_CLEAR– flag Add this to the flags field to indicate that the notification should not be

cleared by the "Clear notifications" button

Page 14: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Emulator note

• Many of the additional flags do nothing with the emulator– There is no default sounds, it's set to silent– no lights or vibration either, of course.

Page 15: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Dialog

• A dialog is always created and displayed as a part of an Activity. – You should normally create dialogs from within

your Activity's onCreateDialog(int) callback method

Page 16: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Dialog

• Using the createDialog() method– All dialog creation is done there.– Only called the first time specfic dialog is created.

• Use the onPrepareDialog(int, Dialog)– Every time the dialog is called, you can modify it

• In you activity:– showDialog(DIALOG_PAUSED_ID);• Where is the ID is the dialog you want to display.

Page 17: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Creating a Dialog

• There are several dialogs you can create– AlertDialog, which can have up to 3 buttons

– ProgressDialog

– DatePickerDialog and TimePickerDialog

– Custom dailog, which you create the layout.

Page 18: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

AlertDialog

• Use the AlertDialog.Builder to create the dialog– You can have up to three buttons, “positive”,

“negative” and cancel– To create the following Dialog• Set the positive and negative, and disable cancel.

Page 19: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

AlertDialog (2)• Code: with two listeners, one for each button.AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setMessage("Winner!")

.setCancelable(false)

.setPositiveButton("Next Level", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int id) {dialog.dismiss();//next(); }}).setNegativeButton("Quit", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int id) {finish();}});

dialog = builder.create();

Page 20: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

AlertDialog (3)

• Instead of buttons, you can have it as radio buttons (and more then just three);– Use Builder.setSignleChoiceItems• Where we send a CharSequence[] of items.

• final String[] items = {"Remove Walls", "Add Walls", "Add/Remove Objects", "Add/Remove Score"};

Page 21: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

AlertDialog (4)• code:final String[] items = {"Remove Walls", "Add Walls", "Add/Remove Objects", "Add/Remove Score"};AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("Choose Type:");builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) {

dialog.dismiss();if (item == 0) { //remove walls}

}});dialog = builder.create();

Page 22: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Custom Dialogs

• You need to create a layout xml file for the dialog– Then dialog.setContentView(R.layout.youlayout)– Using dialog.findViewByID(R.id.X) to access each

widget to set images and/or listeners as necessary– dialog.setTitle(“your title”);

– Now it’s ready to be shown.

Page 23: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Preferences activity

• The activity itself is very easy.– Extend the prefrenceActivity and override the

onCreate – The preferences are kept in xml document.• Use the addPreferenceFromResourcesto use the xml file.

Page 24: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Preferences activity (2)

• Code:public class preferences extends PreferenceActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preference); }}

Page 25: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Preferences xml

• You need to create an xml for the preferences– Preferences are kept be application sessions by default.

• <PreferenceScreen …>– <PreferenceCategory …>– … Blocks Options in this case– </PreferenceCategory>– <PreferenceCategory …>– … </PreferenceCategory>

• </PreferenceScreen>

Page 26: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

CheckBoxPerference

• On (true) or off (false)– <CheckBoxPreference• android:title=“Sensor”• android:summary=“Move with the tilt”• android:defaultValue=“false” Ie off by default• android:key=“sensorPref”

– key used by application to find out the value.

• />

Page 27: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

EditTextPreference

• <EditTextPreference– android:key="textPref"– android:title="Text Value"– android:defaultValue=""– android:summary="text preference demo"– android:dialogTitle="Text preference example"

• />

Page 28: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

ListPreference

• <ListPreference– android:entries="@array/entries_list_preference"– android:entryValues="@array/

entryvalues_list_preference"– android:dialogTitle="list preference example“– android:summary="List preference demo" – android:key="list_preference"– android:title="list perference"

• />

Page 29: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Arrays and xml

• In the values directory– created a file called

“arrays.xml”<resources><string-array name="entries_list_preference"> <item>Alpha Option 01</item> <item>Beta Option 02</item> item>Charlie Option 03</item> </string-array>

<string-array name="entryvalues_list_preference"> <item>alpha</item> <item>beta</item> <item>charlie</item> </string-array></resources>

Page 30: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Access the preference

• In your java code, likely in onResume(…)– onResume is called when you app gets the screen

back from the preferences screen!– Also called when the application starts.– code look something like this:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences (getBaseContext());

– Now you get the preferences with • abstract Map<String, ?> getAll() OR• getBoolean(key, defaultvalue), getInt(key,defvalue),

getString(key, defaultvalue), …

Page 31: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

preference example

• prefs declared like on last slideboolean useSensor = prefs.getBoolean(“sensorPref” , false);– get the sensorPref key, if not found, default is false.

string text = prefs.getString(“textPref”, “”);string list = prefs.getString(“list_preference”,””);– Note, if you had set integer or floats as the

entryValues, then you want to get them as getInt or getFloat.

Page 32: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

Preferences android:summary

• It maybe that instead of you want a the current value listed, instead of the descriptions as I have listed here.

• You will need to implement a listener in PreferenceActivity called OnSharedPreferenceChangeListener

• See the dialog example for how to implement the code.

Page 34: Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first

QA&