1 / 15

Cosc 4755

Cosc 4755. Android Notifications. Notifications. There are a couple of ways to notify users with 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();

stamos
Download Presentation

Cosc 4755

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Cosc 4755 Android Notifications

  2. Notifications • There are a couple of ways to notify users with 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 NotificationManagermNotificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE); int icon = R.drawable.icon; CharSequencetickerText = "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(); CharSequencecontentTitle = "My notification"; CharSequencecontentText = "Hello World!"; Intent notificationIntent = new Intent(this, <Activity name>.class); PendingIntentcontentIntent = 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_NOTIFICATION_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'sContentProvider: • 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 <uses-permission android:name="android.permission.VIBRATE"></uses-permission> 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. Q A &

More Related