Icationicationmanager and notification usage Summary

Source: Internet
Author: User

Twigee'sSource code, One of the functions to be added is the resident notification column, which can only appear in the "Notification" group when previously written, I want to put it in the "running" group, but I don't know how to put it. I checked the official document and found the method. I just needed to add "flag_ongoing_event" to the flags field of notification. At the same time, I also summarized the usage of notification. See the following for details:

(1) Use the system-defined notification

The following is an exampleCode:

// Create a icationicationmanager reference

String NS = context. icationication_service;

Icationicationmanager mnotificationmanager = (notificationmanager) getsystemservice (NS );

// Define various attributes of notification

Int icon = R. drawable. Icon; // notification icon

Charsequence tickertext = "hello"; // notification text prompt displayed in the status bar

Long when = system. currenttimemillis (); // the time when a notification is generated. It is displayed in the notification information.

// Use the above attributes to initialize noication

Notification = new notification (icon, tickertext, when );

/*

* Add sound

* Notification. defaults | = notification. default_sound;

* You can also use the following methods:

* Notification. Sound = URI. parse ("file: // sdcard/notification/ringer.pdf ");

* Notification. Sound = URI. withappendedpath (audio. Media. internal_content_uri, "6 ");

* If you want to repeat the sound until the user responds to the notification, you can add "flag_insistent" to the flags field of notification"

* If the notification ults field of notification includes the "default_sound" attribute, this attribute overwrites the sound defined in the sound field.

*/

/*

* Add vibration

* Notification. defaults | = notification. default_vibrate;

* You can also define your own vibration mode:

* Long [] vibrate = {0,100,200,300}; // vibration starts after 0 milliseconds. The vibration stops after 100 milliseconds, and then the vibration starts again after 200 milliseconds.

* Notification. vibrate = vibrate;

* The long array can be defined as any desired length.

* If the notification ults field of notification includes "default_vibrate", this attribute overwrites the vibration defined in the vibrate field.

*/

/*

* Add LED light reminder

* Notification. defaults | = notification. default_lights;

* You can use your own led reminder mode:

* Notification. ledargb = 0xff00ff00;

* Notification. ledonms = 300; // Bright Time

* Notification. ledoffms = 1000; // destroy time

* Notification. Flags | = notification. flag_show_lights;

*/

/*

* More feature attributes

* Notification. Flags | = flag_auto_cancel; // click this notification in the notification bar and the notification is automatically cleared.

* Notification. Flags | = flag_insistent; // repeat the sound until the user responds to the notification.

* Notification. Flags | = flag_ongoing_event; // put the notification to the "ongoing" group in the notification bar, that is, "running ".

* Notification. Flags | = flag_no_clear; // indicates that the notification is not cleared after you click "clear notification" in the notification bar,

* // Often used with flag_ongoing_event

* Notification. Number = 1; // The number field indicates the number of current events represented by this notification. It will overwrite the status bar icon

* // To use this field, it must start from 1.

* Notification. iconlevel = ;//

*/

// Set the notification event message

Context context = getapplicationcontext (); // Context

Charsequence contenttitle = "my notification"; // the title of the notification bar

Charsequence contenttext = "Hello world! "; // Notification bar content

Intent notificationintent = new intent (this, Main. Class); // click the activity to jump to after the notification

Pendingintent contentintent = pendingintent. getactivity (this, 0, icationicationintent, 0 );

Notification. setlatesteventinfo (context, contenttitle, contenttext, contentintent );

// Pass the notification to icationicationmanager

Micationicationmanager. Y (0, notification );

To update a notification, you only need to call setlatesteventinfo () again after setting the notification, and then resend the notification, that is, call notify () again ().

(2) Use custom notification

To create a custom notification, you can use remoteviews. To define your own extended message, first initialize a remoteviews object, pass it to the contentview field of notification, and then pass pendingintent to the contentintent field. The following sample code is a complete step:

// 1. Create a custom message layout view. xml

<? XML version = "1.0" encoding = "UTF-8"?>

<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "fill_parent" Android: layout_height = "fill_parent">

<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 = "#000"/>

</Linearlayout>

// 2.ProgramThe remoteviews method is used in the Code to define the image and text. Upload the remoteviews object to the contentview field.

Remoteviews contentview = new remoteviews (getpackagename (), R. layout. View );

Contentview. setimageviewresource (R. Id. Image, R. drawable. Icon );

Contentview. settextviewtext (R. Id. Text, "hello, this message is in a custom expanded view ");

Notification. contentview = contentview;

// 3. Define an intent for the contentintent field of notification (note that the setlatesteventinfo () method is not required to use a custom view)

Intent icationicationintent = new intent (this, Main. Class );

Pendingintent contentintent = pendingintent. getactivity (this, 0, icationicationintent, 0 );

Notification. contentintent = contentintent;

// 4. Send a notification

Micationicationmanager. Y (2, notification );

//The following are all sample codes.

// Create a icationicationmanager reference

String NS = context. icationication_service;

Icationicationmanager mnotificationmanager = (notificationmanager) getsystemservice (NS );

// Define various attributes of notification

Int icon = R. drawable. Icon; // notification icon

Charsequence tickertext = "hello"; // notification text prompt displayed in the status bar

Long when = system. currenttimemillis (); // the time when a notification is generated. It is displayed in the notification information.

// Use the above attributes to initialize noication

Notification = new notification (icon, tickertext, when );

Remoteviews contentview = new remoteviews (getpackagename (), R. layout. View );

Contentview. setimageviewresource (R. Id. Image, R. drawable. iconempty );

Contentview. settextviewtext (R. Id. Text, "Hello, this is JC ");

Notification. contentview = contentview;

Intent icationicationintent = new intent (this, Main. Class );

Pendingintent contentintent = pendingintent. getactivity (this, 0, icationicationintent, 0 );

Notification. contentintent = contentintent;

// Pass the notification to icationicationmanager

Micationicationmanager. Y (0, notification );

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.