Usage of Notification and icationicationmanager

Source: Internet
Author: User

Notification is a way for an application to notify users. It does not need to be active and is centrally managed by the notification manager. The notification includes the following functions: 1. create a new status bar icon 2. display additional information in the extended status bar window (an intent can be initiated) 3. flashing/LED4. shake the phone 5. A sound (ringtone, media repository song) Notification manager is a system service used to process notifications. You can use the getSystemService method to obtain references to it, as shown below: icationicationmanager = (icationicationmanager) context. getSystemService (android. content. context. NOTIFICATION_SERVICE); by using the notification manager, you can trigger new notifications, modify existing notifications, or delete those that are no longer needed. First, create a new Notification object and pass it to the icon to be displayed in the status bar, click Text in the status bar, and the Notification time. You can set the number attribute of the Notification object to display the number of events represented by the status bar icon. Notification notification = new Notification (R. drawable. icon, "In EoeAndroidReceiver1", System. currentTimeMillis (); you can configure the Notification appearance in the extended status window in two ways. 1. Use the setLatestEventInfo method to update the details displayed in the standard extended status notification display. 2. Use a Remote View to set contentView and contentIntent so that a custom UI can be assigned to the extended status display. The simplest method is to use the setLatestEventInfo method to fill in the default state window layout. The standard extended status window layout displays the icons and times defined in the constructor, as well as the title and a detailed information string. Notification. setLatestEventInfo (context, "In EoeAndroidReceiver1", null, contentIntent); notifications are often used to request user actions or attention, so you can specify a PendingIntent, trigger a notification when you click it. In most cases, the intent should open the application and navigate to the activity that provides context for the notification. PendingIntent contentIntent = PendingIntent. getActivity (context, 0, new Intent (context, ActivityMain. class), 0); notification. setLatestEventInfo (context, "In EoeAndroidReceiver1", null, contentIntent); icationicationmanager. Y (ication_id _id, notification); the simplest and most consistent way to add sound, flashing, and vibration effects to the notification is to use the default settings of the current user, use the ults attribute, and can be combined: notification. DEFAULT_LIGHTSNotification.DEFAULT_SOUNDNotification.DEFAULT_VIBRATE Use the default value. You can use the Notification. DEFAULT_ALL constant. Notification. defaults = policicaiton. DEFAULT_ALL; by assigning a location URI to the sound attribute, android can play any audio file on the mobile phone as a Notification. To use custom audio, copy it to the device or include it in raw. Uri uri = RingtoneManager. getDefaultUri (RingtongManager. TYPE_NOTIFICATION); Notification. sound = uri; you can use the telephone vibration function to let the Notification execute a specified type of vibration. Android can control the vibration type and use vibration to transmit information or attract users' attention. To set the vibration type, you can assign an array of the longs type to the vibrate attribute of the notification: This array is constructed so that the value representing the vibration time and the value representing the pause time can coexist. To use vibration, you must add the following permissions: <uses-permissionandroid: name = "android. permission. VIBRATE "/> Long [] vibrate = newlong [] {1000,}; notification. vibrate = vibrate; the notification can also contain the properties used to configure the color and flicker frequency of the LED of the device. Each device may have different limits on LED control. If the specified color is available, a color closest to the specified color is used. The ledARGB attribute can be used to set the LED color, while the ledOffMS and ledOnMS attributes can set the LED flashing frequency and mode. You can set the ledOnMS attribute to 1 and the ledOffMS attribute to 0 to enable the LED, or you can disable the LED by setting both attributes to 0. Once the LED settings are configured, The FLAG_SHOW_LIGHTS flag must be added to the flags attribute of the notification. Notification. ledARGB = Color. REDnotification. ledOffMS = 0; notification. ledOnMS = 1; notification. flags = notification. flags | Notification. FLAG_SHOW_LIGHTS; you can configure the FLAG_INSISTENT and FLAG_ONGOING_EVENT tags of the notification as continuous or continuous. Continuous notifications repeat audio, vibration, and flashing settings until they are canceled. 1. icationicationmanager and Notification are used to set notifications. The Notification setting and other operations are relatively simple. The basic method is to create a Notification object and set the Notification parameters, then, use the icationicationmanager service running in the system background to send the notification. The basic steps are as follows: 1) Obtain icationicationmanager: String ns = Context. NOTIFICATION_SERVICE; NotificationManager micationicationmanager = (NotificationManager) getSystemService (ns); 2) create a new Notification object: Notification notification = new Notification (); notification. icon = R. drawable. notification_icon; you can also create Notification in a slightly more complex way: int icon = R. drawable. notification_icon; // notification icon CharSequence tickerText = "Hello"; // Status Bar (Status Bar) The notification text prompt long when = System. currentTimeMillis (); // The time when the Notification is generated. The notification Notification = new Notification (icon, tickerText, when) is displayed in the Notification information. 3) Fill in the attributes of notification: context context = getApplicationContext (); CharSequence contentTitle = "My notification"; CharSequence contentText = "Hello World! "; Intent icationicationintent = new Intent (this, MyClass. class); PendingIntent contentIntent = PendingIntent. getActivity (this, 0, icationicationintent, 0); notification. setLatestEventInfo (context, contentTitle, contentText, contentIntent); Notification provides a wide range of mobile phone prompts: a) notification text prompts displayed in the Status Bar, such as Notification. tickerText = "hello"; B) sends a prompt, for example, notification. defaults | = Notification. DEFAULT_SOUND; notificat Ion. sound = Uri. parse ("file: // sdcard/notification/ringer.pdf"); notification. sound = Uri. withAppendedPath (Audio. media. INTERNAL_CONTENT_URI, "6"); c) Mobile Phone vibration, such as notification. defaults | = Notification. DEFAULT_VIBRATE; long [] vibrate = {0,100,200,300}; notification. vibrate = vibrate; d) LED light flashes, such as notification. defaults | = Notification. DEFAULT_LIGHTS; or you can use your own LED reminder mode: notification. ledARGB = 0xff00ff00; notif Ication. ledOnMS = 300; // bright time notification. ledOffMS = 1000; // destroy time notification. flags | = Notification. FLAG_SHOW_LIGHTS; * more feature attributes * notification. flags | = FLAG_AUTO_CANCEL; // click the notification in the notification bar to automatically clear the notification * notification. flags | = FLAG_INSISTENT; // repeat the sound until the user responds to this notification * notification. flags | = FLAG_ONGOING_EVENT; // put this notification in the notification bar, that is, "Ongoing" in the "running" Group * notification. flags | = FLAG_NO_CLEAR; // indicates that the notification is not cleared after you click "clear notification" in the notification bar. * // It is often used with FLA G_ONGOING_EVENT use * notification together. number = 1; // The number field indicates the number of current events represented by this notification. It will overwrite the number at the top of the status bar icon * // if you want to use this field, it must start from 1 * notification. iconLevel =; // * // set the event message Context context = getApplicationContext (); // Context CharSequence contentTitle = "My Notification "; // notification bar title CharSequence contentText = "Hello World! "; // Notification bar content Intent icationicationintent = new Intent (this, Main. class); // ActivityPendingIntent contentIntent = PendingIntent to jump to after clicking this notification. getActivity (this, 0, icationicationintent, 0); notification. setLatestEventInfo (context, contentTitle, contentText, contentIntent); // pass the Notification to icationicationmanagermicationicationmanager. notify y (0, notification); 4) send notification: private static final int ID_NOTIFICATION = 1; m1_icat IonManager. Y (ID_NOTIFICATION, notification); 2. To update a notification, you only need to call setLatestEventInfo after setting notification, and then send a notification again. 3. For details about the custom Notification View, refer to the official documentation. AndroidSDK: docs/guide/topics/ui/notifiers/icationications.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.