Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first."— Presentation transcript:

1 Cosc 4730 Android Dialogs and notifications

2 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.

3 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).

4 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.

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

6 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.

7 Example code Context context = getApplicationContext(); CharSequence contentTitle = "My notification"; CharSequence contentText = "Hello World!"; Intent notificationIntent = new Intent(this,.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

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

9 Canceling notifications To cancel a specific notification mNotificationManager.cancel(YOURAPP_NOTIFI CATION_ID); To cancel all of the notifications mNotificationManager.cancelAll();

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

11 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 in the AndroidManifest.xml

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

13 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

14 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.

15 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

16 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.

17 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.

18 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.

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

20 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"};

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

22 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.

23 Preferences activity The activity itself is very easy. – Extend the prefrenceActivity and override the onCreate – The preferences are kept in xml document. Use the addPreferenceFromResources to use the xml file.

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

25 Preferences xml You need to create an xml for the preferences – Preferences are kept be application sessions by default. – – … Blocks Options in this case – – …

26 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. />

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

28 ListPreference <ListPreference – android:entries="@array/entries_list_preference" – android:entryValues="@array/entryvalues_list_pr eference" – android:dialogTitle="list preference example“ – android:summary="List preference demo" – android:key="list_preference" – android:title="list perference" />

29 Arrays and xml In the values directory – created a file called “arrays.xml” Alpha Option 01 Beta Option 02 item>Charlie Option 03 alpha beta charlie

30 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 getAll() OR getBoolean(key, defaultvalue), getInt(key,defvalue), getString(key, defaultvalue), …

31 preference example prefs declared like on last slide boolean 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.

32 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.

33 preferences xml. Only covered a subset of the preferences xml, including some interfaces. see http://developer.android.com/reference/android /preference/Preference.html for more information. http://developer.android.com/reference/android /preference/Preference.html While out some what out of date, this is very helpful with for changing the android:summary – http://stackoverflow.com/questions/531427/how-do- i-display-the-current-value-of-an-android-preference- in-the-preference-su http://stackoverflow.com/questions/531427/how-do- i-display-the-current-value-of-an-android-preference- in-the-preference-su

34 Q A &


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

Similar presentations


Ads by Google