. Android Notification Details

Source: Internet
Author: User

According to the lifecycle of the activity, when the activity is not displayed, the onStop function (such as pressing the home Key) will be executed. Therefore, you put the notification in the notification bar in the onStop function (except press the return key, remove the notification from the notification bar. Or, the notification bar icon is displayed as long as the program is running.

The following describes some constants, fields, and methods in the Notification class:
Constant:
DEFAULT_ALL use all default values, such as sound, vibration, and pop-up.
Ult_lights use default flash prompt
DEFAULT_SOUNDS use the default sound prompt
DEFAULT_VIBRATE
[Note]: to add a mobile phone to vibrate, you must add the permission to manifest. xml:
<Uses-permission android: name = "android. permission. VIBRATE"/>
The above effect constants can be superimposed through
Notification. defaults = DEFAULT_SOUND | DEFAULT_VIBRATE;
Notification. defaults | = DEFAULT_SOUND (it is best to test it on a real machine, not on the simulator of the vibration effect)

// Set the flag bit
FLAG_AUTO_CANCEL should be cleared by the clear button of the status bar.
FLAG_NO_CLEAR this notification can be cleared by the clear button of the status bar.
FLAG_ONGOING_EVENT notification is placed in the running
Whether the FLAG_INSISTENT is always playing. For example, if the music is always playing, you must know the user's response.

Common fields:
ContentIntent sets the PendingIntent object. This Intent is sent when you click it.
Defaults add default Effect
Flags: flag bit, for example, FLAG_NO_CLEAR
Icon setting icon
Sound set sound
TickerText text displayed in the status bar
The timestamp when the notification is sent

Icationicationmanager common methods:
Public void cancelAll () removes all notifications (only for notifications under the current Context)
Public void cancel (int id) removes notifications marked as IDS (only for all notifications under the current Context)
Public void notify (String tag, int id, Notification notification) adds the Notification to the status bar. The label is tag and marked as id.
Public void notify (int id, Notification notification) adds the Notification to the status bar and marks it as id

Package com. ljq. activity; import android. app. activity; import android. app. notification; import android. app. icationicationmanager; import android. app. pendingIntent; import android. content. intent; import android. graphics. color; import android. OS. bundle; public class MainActivity extends Activity {/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); clearNotification () ;}@ Override protected void onStop () {showNotification (); super. onStop () ;}@ Override protected void onStart () {clearNotification (); super. onStart ();}/*** display notification in the status bar */private void showNotification () {// create a icationicationmanager reference icationicationmanager = (icationicationmanager) this. getSystemService (android. content. context. NOTIFICATION_SERVICE); // defines the attributes of Notification. Notification notification = new Notification (R. drawable. icon, "Supervisor System", System. currentTimeMillis ()); // FLAG_AUTO_CANCEL: This notification can be cleared by the clear button of the status bar. // FLAG_NO_CLEAR: This notification cannot be cleared by the clear button of the status bar. // FLAG_ONGOING_EVENT: the notification is being run. // FLAG_INSISTENT:, for example, the music is always played and the user knows to respond to notification. flags | = Notification. FLAG_ONGOING_EVENT; // place the notification in the notification bar, that is, notification in the running group. flags | = Notification. FLAG_NO_CLEAR; // indicates that the notification is not cleared after you click "clear notification" in the notification bar. The notification is often used with FLAG_ONGOING_EVENT. flags | = Notification. FLAG_SHOW_LIGHTS; // DEFAULT_ALL use all default values, such as sound, vibration, and pop-up. // DEFAULT_LIGHTS use the default flash prompt // DEFAULT_SOUNDS use the default prompt sound // DEFAULT_VIBRATE use the default phone shake, <uses-permission android: name = "android. permission. VIBRATE "/> Permission notification. defaults = Notification. DEFAULT_LIGHTS; // constant of the superposition effect // notification. defaults = Notification. DEFAULT_LIGHTS | Notification. DEFAULT_SOUND; notification. ledARGB = Color. BLUE; notification. ledOnMS = 5000; // flash time, millisecond // set the notification event message CharSequence contentTitle = "Supervisor System title"; // notification bar title CharSequence contentText = "Supervisor System content "; // Intent notificationIntent = new Intent (MainActivity. this, MainActivity. class); // The Activity PendingIntent contentItent = PendingIntent to jump to after clicking this notification. getActivity (this, 0, icationicationintent, 0); notification. setLatestEventInfo (this, contentTitle, contentText, contentItent); // transmits the Notification to icationicationmanager. Y (0, notification );}
// Delete notification private void clearNotification () {// Delete the notification NotificationManager defined previously (notificationManager) this. getSystemService (icationication_service); icationicationmanager. cancel (0 );}}

 

 

Related Article

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.