android notifications pdf by apk855

VIEWS: 18 PAGES: 20

More Info
									                  Notifications


                            Copyright © 2012, apkdownloads.in. All rights
NOTIFICATIONS-1
                                                               reserved
     • Several types of situations may arise that require
       you to notify the user about an event that occurs in
       your application.
     • Some events require the user to respond and others
       do not.
     • For example:
           When an event such as saving a file is complete, a small
            message should appear to confirm that the save was
            successful.
           If the application is running in the background and needs
            the user's attention, the application should create a
            notification that allows the user to respond at his or her
            convenience.
           If the application is performing work that the user must wait
            for (such as loading a file), the application should show a
            hovering progress wheel or bar.

                                                  Copyright © 2012, apkdownloads.in. All rights
NOTIFICATIONS-2
                                                                                     reserved
     • Each of these notification tasks can be achieved
       using a different technique:

     • A Toast Notification for brief messages that come
       from the background.

     • A Status Bar Notification for persistent reminders
       that come from the background and request the
       user's response.

     • A     Dialog     Notification   for         Activity-related
       notifications.

                                             Copyright © 2012, apkdownloads.in. All rights
NOTIFICATIONS-3
                                                                                reserved
                         Toast Notification

     • A toast notification is a message that pops up on the
        surface of the window.
     • It only fills the amount of space required for the message
        and the user's current activity remains visible and
        interactive.
     • The notification automatically fades in and out, and does
        not accept interaction events.
     • Because         a  toast   can     be  created     from  a
        background Service, it appears even if the application
        isn't visible.
     A toast is best for short
      text messages, such as "File saved,“
     when you're fairly certain the user is
      paying attention to the screen.
     A toast can not accept user
      interaction events
                                            Copyright © 2012, apkdownloads.in. All rights
NOTIFICATIONS-4
                                                                               reserved
     • A toast can be created and displayed from an
       Activity or a Service.
     • If you create a toast notification from a Service, it
       appears in front of the Activity currently in focus.
     • First, instantiate a Toast object with one of the
       makeText() methods.
     • This method takes three parameters:
           the application Context
           the text message
           the duration for the toast
     • It returns a properly initialized Toast object.
     • You can display the toast notification with show()

                                           Copyright © 2012, apkdownloads.in. All rights
NOTIFICATIONS-5
                                                                              reserved
     Example:
       Context context = getApplicationContext();
       CharSequence text = "Hello toast!";
       int duration = Toast.LENGTH_SHORT;

       Toast toast = Toast.makeText(context, text, duration);
       toast.show();
     • The entire statements can be written in a single
       statements.
     • Toast.makeText(context, text, duration).show();
     • Example:
     •   Toast.makeText(getApplicationContext(),”hello welcome”,
         Toast.LENGTH_LONG);


                                                Copyright © 2012, apkdownloads.in. All rights
NOTIFICATIONS-6
                                                                                   reserved
                          Positioning your Toast

     • A standard toast notification appears near the bottom of
       the screen, centered horizontally.

     • You can change this position with the setGravity(int, int,
       int) method.

     •   This accepts three parameters: a Gravity constant, an x-
         position offset, and a y-position offset.

     • For example:
          • toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);




                                                   Copyright © 2012, apkdownloads.in. All rights
NOTIFICATIONS-7
                                                                                      reserved
                     Creating a Custom Toast View

     • if a simple text message isn't enough, you can create a
       customized layout for your toast notification.
     • To create a custom layout, define a View layout, in XML
       or in your application code, and pass the
       root View object to the setView(View) method.
     • For example,
     • You can create the layout
          for the toast visible
           in the screenshot by
          Configuring it by using
          Xml file



                                            Copyright © 2012, apkdownloads.in. All rights
NOTIFICATIONS-8
                                                                               reserved
                            toast_layout.xml
     •   <LinearLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
                 android:id="@+id/toast_layout_root"
                 android:orientation="horizontal"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:padding="10dp"
                 android:background="#DAAA"
                 >
           <ImageView android:id="@+id/image"
                  android:layout_width="wrap_content"
                  android:layout_height="fill_parent"
                  android:layout_marginRight="10dp"
                  />
           <TextView android:id="@+id/text"
                 android:layout_width="wrap_content"
                 android:layout_height="fill_parent"
                 android:textColor="#FFF"
                 />
         </LinearLayout>


                                                Copyright © 2012, apkdownloads.in. All rights
NOTIFICATIONS-9
                                                                                   reserved
                                   Activity File

         LayoutInflater inflater = getLayoutInflater();
         View layout = inflater.inflate(R.layout.toast_layout,
                            (ViewGroup) findViewById(R.id.toast_layout_root));

         ImageView image = (ImageView) layout.findViewById(R.id.image);
         image.setImageResource(R.drawable.android);
         TextView text = (TextView) layout.findViewById(R.id.text);
         text.setText("Hello! This is a custom toast!");

         Toast toast = new Toast(getApplicationContext());
         toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
         toast.setDuration(Toast.LENGTH_LONG);
         toast.setView(layout);
         toast.show();




                                                       Copyright © 2012, apkdownloads.in. All rights
NOTIFICATIONS-10
                                                                                          reserved
                            Status Bar Notifications
     • A status bar notification adds an icon to the system's
       status bar (with an optional ticker-text message) and a
       notification message in the notifications window.
     • When the user selects the notification, Android fires
       an Intent that is defined by the Notification (usually to
       launch an Activity).
     • You can also configure the notification to alert the user
       with a sound, a vibration, and flashing lights on the
       device.
     • A status bar notification should be used for any case in
       which a background service needs to alert the user about
       an event that requires a response.
     • A background service should never launch an activity on
       its own in order to receive user interaction.
     • The service should instead create a status bar
       notification that will launch the activity when selected by
       the user.                                Copyright © 2012, apkdownloads.in. All rights
NOTIFICATIONS-11
                                                                                      reserved
     • The above figure shows the Notification icon on the
       left hand side.
     • The below figure shows the notification’s message in
       the notification window.




                                         Copyright © 2012, apkdownloads.in. All rights
NOTIFICATIONS-12
                                                                            reserved
                         Points about Notification
     • An Activity or Service can initiate a status bar notification.
     • Because an activity can perform actions only while it is
       running in the foreground and its window has focus, you will
       usually create status bar notifications from a service.
     •   This way, the notification can be created from the
       background, while the user is using another application or
       while the device is asleep.
     •   To create a notification, you must use two classes
       Notification and NotificationManager.
     • We use Notification instance to define the properties of the
       status Bar of Notification such as:
           Status bar icon
           Notification message.
           Extra settings such as a sound file to display.

                                                      Copyright © 2012, apkdownloads.in. All rights
NOTIFICATIONS-13
                                                                                         reserved
                             NotificationManager

     • NotificaitonManager is an Android service that
       executes and manages all the status bar
       notifications.
     • You do not instantiate the NotificationManager
       directly.
     • We get a reference by calling getSystemService() .
     • To notify the user , pass it to Notification by using
       notify() method.
     • Getting a reference of NotificationManager:
          String ns=Context.NOTIFICATION_SERVICE;
          NotificationManager nm=(NotificationManager)getSystemService(ns);
     • Instantiate the Notification:
           Notification notification=new Notification(R.drawable.icon, “Simple
            Notification”,System.CureentTimeMillis());

                                                      Copyright © 2012, apkdownloads.in. All rights
NOTIFICATIONS-14
                                                                                         reserved
                   Defining the Notification Message and
                               Pending Intent
     • After displaying the notification to perform a
       particular action, we use the PendingIntent class.
     • Example:
           Context context=getApplicationContext();
          CharSequence contentTitle=“My Notificaiton”;
          CharSequence contentText=“Hello World”;
          Intent notificationIntent=new Intent(this, MyClass.class);
          PendingIntent contentIntent=PendingIntent.getActivity(this,0,
             notificationIntent, 0);

             notification.setLatestEventInfo(context,contentTitle,content
             Text, contentIntent);


                                                   Copyright © 2012, apkdownloads.in. All rights
NOTIFICATIONS-15
                                                                                      reserved
                      Passing Notification to the
                        NotificationManager

          private static final int HELLO_ID=1;
          mNotificationManager.notify(HELLO_ID,notification);
     • The first parameter defines the unique ID for the
       notification.
     • The ID is used to identify the particular notification in
       your application.
     • When you want to take an action on a particular
       notification it ll be identified by using a unique ID .
     • Second Parameter defines the notification object to
       be displayed as notification.
     • We can clear the notification when the user selects
       it from the notification window.
     • Add FLAG_AUTO_CANCEL flag to Notification.
                                                 Copyright © 2012, apkdownloads.in. All rights
NOTIFICATIONS-16
                                                                                    reserved
                   Clearing Notifications manually

     • To clear the notifications manually , we use the
       following methods.
           cancel(int).
           cancelAll().
     •  cancel(int ) clears the notification with the passed
       int as notification ID.
     • cancelAll() clears all the notifications in the
       notification window.




                                         Copyright © 2012, apkdownloads.in. All rights
NOTIFICATIONS-17
                                                                            reserved
                   Optional Settings for the Notifications

     • Apart from displaying a text and icon for a
       notification, we can use the following optional
       settings for a notification.
             A ticker-text message for the status bar.
             An alert sound.
             A vibrate setting.
             A flashing LED setting.
     • Adding a sound:
           You can alert the user with the default notification sound (
            which is defined by the user ) or with a sound specified by
            your application.
           To use the user’s default sound, add “ DEFAULT_SOUND”
            to the defaults field.
           notification.defaults |=Notification.DEFAULT_SOUND.
                                                     Copyright © 2012, apkdownloads.in. All rights
NOTIFICATIONS-18
                                                                                        reserved
          • To use a different sound with your notifications, pass a Uri
            reference to the sound field.
          • Example:

             notification.sound=Uri.parse(file:///sdcard/notification/ring
            er.mp3)
          • Adding Vibration:
               —You can alert the user with the default vibration pattern or with
                a vibration pattern by your application.
               —To use the default pattern, and DEFAULT_VIBRATE to the
                defaults field.
               notificaiton.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;



                                                         Copyright © 2012, apkdownloads.in. All rights
NOTIFICATIONS-19
                                                                                            reserved
     • Adding flashing lights:
          • To alert the user by flashing LED lights, you can implement
            the default light pattern (if available), or define your own color
            and pattern for the lights.
          • To use the default light settings:
                   notificaitons.defaults |=Notifications.DEFAULT_LIGHTS;
          • To define your own color pattern, define the following fields:
               —     ledARGB ( field for color )
               —     ledOffMS ( length of time , in milliseconds , to keep the light off )
               —     ledOnMS ( length of time , in milliseconds, to keep the light on )
               —     FLAG_SHOW_LIGHTS; (
          • Example:
               —     notification.ledARGB= 0Xff00ff00;
               —     notification.ledOnMS=300;
               —     notification.ledOffMS=1000;
               —     notification.flags |= Notification.FLAG_SHOW_LIGHTS;

                                                              Copyright © 2012, apkdownloads.in. All rights
NOTIFICATIONS-20
                                                                                                 reserved

								
To top