Intents and Notifications

Intents
  •  Intents are the messages that are used to activate following application components
  1.  Activities
  2.  Services
  3.  Broadcast Receivers
  • Intent is an object, which has a data structure holding any one of the following data :
  1. Abstract description of an operation to be performed
  2.  In the case of broadcasts, a description of something that has happened and is being announced.
Invoking Components
  • Application components are invoked through intents in the following way :
  •  Activity : Intent object is passed to following functions to invoke the Activity.
  1.  Context.startActivity (Intent intent)
  2.  Activity.startActivityForResult (Intent intent, int requestCode)
  • Service : Intent object is passed to following functions to invoke the Service.
  1. Context.startService (Intent)
  2. Context.bindService (Intent)
  • Broadcast Receivers : Intent object is passed to following functions to invoke the Broadcast Receiver.
  1. Context.sendBroadcast ( Intent intent, String receiverPermission)
  2. Context.sendOrderedBroadcast ( Intent intent, String receiverPermission)
  3. Context.sendStickyBroadcast ( Intent intent )
  •  Android system finds the appropriate application component to respond to the intent, instantiating them if necessary.
  • There is no overlap within these messaging systems: Broadcast intents are delivered only to broadcast receivers, never to activities or services.
Intent Object
  •  An Intent object is a bundle of information. It basically contains following data:
  1.  Component name
  2.  Action
  3.  Data
  4.  Category
  5.  Instructions on launching a target activity
Intent : Component Name
  •  Component name specifies the name of the component that should handle the intent. It has following conditions:
  1.  This field is a combination of the fully qualified class name of the target component.
  2.  The component name is optional. If it is set, the Intent object is delivered to an instance of the designated class else Android uses other information in the Intent object to locate a suitable target
  •  The component name is set/read using following methods of the intent object:
  1. setComponent()
  2. setClass()
  3. setClassName()
  4. getComponent()
Intent : Action
  •  Action is a string that specifies the work to be performed by an Activity or a Service and specifies an information being reported in case of Broadcast receivers.
  •  Action largely determines how the rest of the intent is structured particularly the data and extras fields.
  •  Action names are usually coupled tightly to the other fields of the intent.
  •  The action in an Intent object is set/read by the following methods
  1.  setAction()
  2.  getAction()
Intent : Data
  •  This specifies the URI of the data to be acted on and the MIME type of that data.
  •  Data specifications are tightly coupled with Action type associated with the Intent object for example :
  1.  Action field ACTION_EDIT would have a data field that would contain the URI of the document to be displayed for editing.
  •  When matching an intent to a component that is capable of handling the data, it's often important to know the type of data (its MIME type) in addition to its URI. For example:
  1.  A component able to display image data should not be called upon to play an audio file.
  •  setData(), setType(), setDataAndType(), getData() and getType() methods are used to set/get the data and its type on intent objects.
Intent Category
  1.  This information of the intent object specifies the kind of the component that should handle the intent. 
  2. Any number of category descriptions can be placed in an Intent object.
Example :
  •  CATEGORY_HOME
  •  CATEGORY_LAUNCHER
  •  CATEGORY_PREFERENCE
  •  CATEGORY_GADGET
  1. Following methods of Intent objects are used to get and set the category:
  •  addCategory()
  •  removeCategory()
  •  getCategories()
Intent : Extras
  1. Extras is Key-value pairs for additional information that should be delivered to the component handling the intent.
  2.  Just as some actions are paired with particular kinds of data URIs, some are paired with particular extras. For example 
  • ACTION_TIMEZONE_CHANGED intent has a "time-zone" extra that identifies the new time zone 
  •  ACTION_HEADSET_PLUG has a "state" extra indicating whether the headset is now plugged in or unplugged
     3.  Extras in the Intent object can be installed and read as a Bundle using the putExtras() and getExtras() methods.

Intent Flags
  1. Flags are used to instruct the Android system about following aspects:
  •  How to launch an activity 
  •  How to treat an activity after it is launched
Intent Resolution
  1.  Explicit Intent :
  • They are used to invoke components within an application. It uses only the component name for identifying the target component.
  • Only the component name field of the intent object is used to resolve the component.
     2.  Implicit Intent :
  • They are generally used to activate components in other applications.
  • Components are resolved by comparing the intent objects with intent filters.
  • Intent filters are structures associated with components, they advertise the capabilities of a component and delimit the intents it can handle.
  • If a component does not have any intent filters, it can receive only explicit intents.
  • A component with filters can receive both explicit and implicit intents.
  • Only three aspects of an Intent object are consulted when the object is tested against an intent filter:
  •  Action , data (both URI and data type) and category
Intent Filters
  1.  One or more intent filters can be associated with services, activities and broadcast receivers to inform the android system about the implicit intents they can honour. 
  2.  Intents filters are associated with services and activities in the manifest file only 
  3.  Intent filters can be associated with broadcast receivers both through manifest file and code.
  4.  Intent filters has fields that are parallel to the action, data and category fields of intent object.

Status Bar Notification

  1. A status bar notification adds an icon to the system's status bar (with anoptional ticker-text message) and an expanded message in the"Notifications" window.
  2. When the user selects the expanded message, Android fires an Intent that is defined by the notification (usually to launch an Activity).
  3. You can also configure the notification to alert the user with a sound, a vibration, and flashing lights on the device.
  4. This kind of notification is ideal when your application is working in a background Service and needs to notify the user about an event. 
  5. To create a notification, following classes should be used:
  • Notification
  • NotificationManager 
  • Notification is a class that represents how a persistent notification is to be presented to the user using the NotificationManager.
  • NotificationManager is an Android system service used to handle notifications.
Create Notification:
  1. Get a reference to the Notification Manager by passing an ID.
                     String ns = Context.NOTIFICATION_SERVICE; 
                     NotificationManager nm = (NotificationManager) getSystemService(ns);
     2.  Instantiate the notification.
                     int icon = R.drawable.notification_icon;
                    CharSequence tickerText = "Hello...";
                    long time = System.currentTimeMillis();
                    Notification notification = new Notification(icon, tickerText, time);
     3. Define the Notification's expanded message and Intent.
                    Context context = getApplicationContext();
                    CharSequence title = "Notification Title";
                    CharSequence text = "Notification Message...";
                    Intent intent = new Intent(this, MyClass.class);
                    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent,0);
                    notification.setLatestEventInfo(context, title, text, contentIntent);
     4. Pass the Notification to the NotificationManager.
                    private static final int NOTIF_ID = 1;
                    nm.notify(NOTIF_ID, notification);
Addiing a Sound:
  • User's default sound
                      notification.defaults |= Notification.DEFAULT_SOUND;
  • Use a different sound
                      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");
  •  If you want the sound to continuously repeat until the user responds to the
                      notification or the notification is cancelled, add "FLAG_INSISTENT" to the flags field as
                      notification.flags = Notification.FLAG_INSISTENT
Adding Vibration:
  • 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;
  • The long array defines the alternating pattern for the length of vibration off and on (in milliseconds).The first value is how long to wait (off) before beginning, the second value is the length of the first vibration, the third is the next length off, and so on. The pattern can be as long as you like, but it can't be set to repeat.
Adding Flashing Lights:
  • Default light setting
                  notification.defaults |= Notification.DEFAULT_LIGHTS;
  • To define your own color and pattern, define values for fields like ledARGB, ledOffMS field (length of time to keep the light off), ledOnMS (length of time 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;
  • In this example above, the green light repeatedly flashes on for 300 milliseconds
and turns off for one second. Not every color in the spectrum is supported by the device LEDs, and not every device supports the same colors, so the hardware estimates to the best of its ability. Green is the most  common notification color.


Comments

  1. Please maintain your blog.....You didn't posted new contents.....

    ReplyDelete

Post a Comment

Please post comments here:-)

Popular posts from this blog

Android Objective type Question and Answers

Android Questions and Answers for written exams

SCJP1.6 Question and Answers