Android Basics Getting Started tutorial--2.5.2 Notification (status bar Notification) Detailed

Source: Internet
Author: User
<span id="Label3"></p>Android Basics Getting Started tutorial--2.5.2 Notification (status bar Notification) Detailed<p><p>Tags (space delimited): android basics Getting Started Tutorial</p></p>Introduction to this Section: <blockquote> <blockquote> <p>This section brings up controls for displaying notification information in the status bar on Android: Notification, I believe most<br>Learning Android is familiar to him, and many of the online tutorials on notification are based on 2.x, and<br>Android devices are now generally more than 4.x, even more than 5.0 of all have; their respective notification.<br>It's not the same! And this section to you to explain is based on 4.x above notification, and more than 5.0 of the notification<br>We'll explain the new features of Android 5.0 in the Advanced tutorials ~<br>Some of the official documents on notification Are:<br><strong>Design Ideas</strong> : Notifications in Android 4.4 and Lower <strong>translation</strong> : Notification<br><strong>API Document</strong> : Notification<br>To visit the above site, you may need a ladder oh ~</p> </blockquote> </blockquote>1. Part of the design document 1) basic layout of notification<p><p></p></p> <blockquote> <blockquote> <p>The constituent elements above are in turn:</p> <ul> <li><strong>Icon/photo</strong>: Large Icons</li> <li><strong>title/name</strong>: Title</li> <li><strong>Message</strong>: Content information</li> <li><strong>Timestamp</strong>: Notification time, default is the time the system notifies, or can be set by Setwhen ()</li> <li><strong>secondary</strong>icon: Small Icons</li> <li><strong>content Text</strong> , a text on the left hand side of a small icon</li> </ul> </blockquote> </blockquote>2) Extended Layout <blockquote> <blockquote> <p>In the Jelly Bean you can provide details of more events for the Notification. You can display the first few lines of a message or a preview of a picture by extending the Layout. This allows the user to see much more content-and sometimes even the entire message. The user can open the extended layout by pinch-zoom or swiping with both Fingers. Android provides two extended layouts (text and images) for a single message for you to use when you develop your app.</p> <p><br></p> <p>about other design things, do not mention, interested in Self-view the above provided API documentation, know the next<br>This notification in more than 4.x version can be a variety of good! We are more concerned about the<br>How to write code using this thing, below we will learn the usage of notification!</p> </blockquote> </blockquote>Basic usage flow of 2.Notification <blockquote> <blockquote> <p>The status notification bar mainly involves 2 classes: Notification and Notificationmanager<br><strong>Notification</strong>: Notification information class, which corresponds to the various properties of the notification bar<br><strong>Notificationmanager</strong>: is the status bar notification management class, responsible for sending notifications, clearing notifications and other Operations.</p> <p>The basic process Used:</p> <ul> <li><strong>Step 1.</strong> Get Notificationmanager object:<br>Notificationmanager Mnmanager = (notificationmanager) Getsystemservice (notification_service);</li> <li><strong>Step 2.</strong> Create a builder construction class for the notification bar:<br>Notification.builder mbuilder = new Notification.builder (this);</li> <li><strong>Step 3.</strong> Settings related to builder, such as title, content, icon, action, etc.</li> <li><strong>Step 4.</strong> Call Builder's Build () method to assign a value of notification</li> <li><strong>Step 5</strong>. Call Notificationmanager's Notify () method to send notifications!</li> <li><strong>PS:</strong> In addition we can call Notificationmanager's Cancel () method for canceling the notification</li> </ul> </blockquote> </blockquote>3. Set up some of the relevant methods: <blockquote> <p><p>Notification.builder mbuilder = new Notification.builder (this);<br>Then call the following related methods to Set: (official API Document: Notification.builder)<br>The usual methods are as follows:</p></p> <ul> <li><li><strong>Setcontenttitle</strong> (charsequence): Set Title</li></li> <li><li><strong>Setcontenttext</strong> (charsequence): Set Content</li></li> <li><li><strong>Setsubtext</strong> (charsequence): sets the text of a small line below the content</li></li> <li><li><strong>Setticker</strong> (charsequence): sets the text message displayed at the top when the notification is received</li></li> <li><li><strong>Setwhen</strong> (long): set the notification time, which is generally set to System.currenttimemillis () when the notification is received</li></li> <li><li><strong>Setsmallicon</strong> (int): set the small icon in the lower right corner, which will be displayed at the top when receiving the Notification.</li></li> <li><li><strong>Setlargeicon</strong> (Bitmap): set the large icon on the left</li></li> <li><li><strong>Setautocancel</strong> (boolean): If the user clicks Notification Click on the panel to let the notification cancel (default is not canceled)</li></li> <li><li><strong>SetDefaults</strong> (int): the simplest and easiest to add sound, strobe, and vibration effects to notifications<br>Using the default (defaults) property, you can combine multiple properties,<br><strong>notification.default_vibrate</strong> (add Default Vibration alert);<br><strong>Notification.default_sound</strong> (add Default Sound reminders);<br><strong>notification.default_lights</strong> (add Default Tri-color Light Reminder)<br><strong>Notification.default_all</strong> (add default of 3 all reminders Above)</li></li> <li><li><strong>setvibrate</strong> (long[]): sets the vibration mode, for example:<br>Setvibrate (new long[] {0,300,500,700}), delay 0ms, then vibrate 300ms, delay 500ms,<br>Then vibrate 700ms, about vibrate usage will be explained later!</li></li> <li><li><strong>setlights</strong> (int argb, int onms, int offms): set three-color lights, the parameters are: light color,<br>Light duration, Dark time, Not all colors can, This is related to equipment, some mobile phones are not with Three-color lights;<br>also, you need to set the flags for notification to notification.flag_show_lights to support Tri-color lamp reminders!</li></li> <li><li><p><strong>Setsound</strong> (Uri): set to receive the notification when the ringtone, you can use the system, or you can set up, examples are as follows:<br>. setdefaults (notification.default_sound)//get Default Ringtone<br>. setsound (uri.parse ("file:///sdcard/xx/xx.mp3"))//get a custom ringtone<br>. setsound (uri.withappendedpath (Audio.Media.INTERNAL_CONTENT_URI, "5"))<br>Get ringtones inside the Android multimedia library</p></li></li> <li><li><p><strong>setongoing</strong> (boolean): set to ture, which indicates that it is an in-progress Notification. They are usually used to denote<br>A background task that the user actively participates in (such as playing Music) or is waiting in some way, so occupy the device (such as a file download,<br>Synchronous operation, Active Network Connection)</p></li></li> <li><li><strong>setprogress</strong> (int,int,boolean): set up a notification with a progress bar<br>The parameters are: the maximum value of the progress bar, the current progress, whether the progress is uncertain<br>If you set a notification for a determined progress Bar: call setprogress (max, progress, false),<br>Notify update progress when the progress is updated and remove the progress bar after the download is complete<br>, by calling Setprogress (0, 0, False) to Both. If the progress bar is indeterminate (continuous activity),<br>This is the indication that the activity is continuing when processing progress is not accurate, so call setprogress (0, 0, True)<br>At the end of the operation, call setprogress (0, 0, False) and update the notification to remove the indicator bar</li></li> <li><li><strong>setcontentintent</strong> (pendingintent): Pendingintent and intent are slightly different, it can set the number of executions,<br>Mainly used for remote service communication, alarm, notification, launcher, sms, in general, with Less. Like here through<br>Pending starts activity:getactivity (Context, int, Intent, int), and of course it can start service or broadcast<br>The bit identifier for the pendingintent (fourth parameter):<br><strong>Flag_one_shot</strong> indicates that the returned pendingintent can only be executed once and automatically canceled after execution<br><strong>Flag_no_create</strong> indicates that if the description of the pendingintent does not exist, the corresponding pendingintent is not created, but returns null<br><strong>flag_cancel_current</strong> indicates that the corresponding pendingintent already exists, cancels the former, and then creates a new pendingintent,<br>This helps keep data up-to-date with communication scenarios that can be used for instant communication<br><strong>Flag_update_current</strong> represents the updated pendingintent<br>Examples of use:</li></li> </ul> </blockquote><pre class="prettyprint"><pre class="prettyprint"><code class="language-Java hljs fsharp"><span class="hljs-comment">//点击后跳转Activity</span><span class="hljs-keyword">new</span> Intent(context,XXX.<span class="hljs-keyword">class</span>); <span class="hljs-number">0</span><span class="hljs-number">0</span>); mBuilder.setContentIntent(pendingIntent) </code></pre></pre> <blockquote> <blockquote> <ul> <li><strong>setpriority</strong> (int): Set Priority:</li> </ul> </blockquote> </blockquote> <table> <thead> <tr> <th align="left">Priority Level</th> <th align="left">User</th> </tr> </thead> <tbody> <tr> <td align="left">MAX</td> <td align="left">Important and urgent notice to inform the user that the event is time-critical or needs to be dealt with immediately.</td> </tr> <tr> <td align="left">High</td> <td align="left">High priority is used for important communication content, such as short messages or chats, which are more interesting to the User.</td> </tr> <tr> <td align="left">DEFAULT</td> <td align="left">The default priority is used for notifications that do not have a special priority Classification.</td> </tr> <tr> <td align="left">Low</td> <td align="left">Low priority can notify the user but is not a very urgent event.</td> </tr> <tr> <td align="left">MIN</td> <td align="left">Used for background messages (such as weather or location information). The lowest priority notification will only display the icon in the status bar, and only the user Drop-down notification drawer can see the Content.</td> </tr> </tbody> </table> <blockquote> <blockquote> <p>Corresponding Attribute: Notification.priority_high ...</p> </blockquote> </blockquote>4. Code example: The most common notification:<p><p>Let's write a simple example to experience the use of notification:</p></p><p><p><strong>Run</strong> :</p></p><p><p></p></p><p><p><strong>Key Code</strong> :</p></p><p><p>Here directly paste <strong>Mainactivity.java</strong> code:</p></p><pre class="prettyprint"><code class="language-Java hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">mainactivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">appcompatactivity</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">View </span>. <span class="hljs-title">Onclicklistener</span> {</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Context mcontext;<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Notificationmanager mnmanager;<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Notification notify1; Bitmap Largebitmap =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>;<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Final</span></span> <span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Notifyid_1 =<span class="hljs-number"><span class="hljs-number">1</span></span>;<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Button btn_show_normal;<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Button btn_close_normal;<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Setcontentview (r.layout.activity_main); Mcontext = Mainactivity.<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>;<span class="hljs-comment"><span class="hljs-comment">//create Large icons for bitmap</span></span>Largebitmap = Bitmapfactory.decoderesource (getresources (), r.mipmap.iv_lc_icon); Mnmanager = (notificationmanager) Getsystemservice (notification_service); BindView (); }<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">BindView</span></span>() {btn_show_normal = (Button) Findviewbyid (r.id.btn_show_normal); Btn_close_normal = (Button) Findviewbyid (r.id.btn_close_normal); Btn_show_normal.setonclicklistener (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>); Btn_close_normal.setonclicklistener (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnClick</span></span>(View V) {<span class="hljs-keyword"><span class="hljs-keyword">Switch</span></span>(v.getid ()) {<span class="hljs-keyword"><span class="hljs-keyword"></span> case</span>R.id.btn_show_normal:<span class="hljs-comment"><span class="hljs-comment">//define A pendingintent Click Notification to start an activity</span></span>Intent it =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Intent (mcontext, otheractivity.class); Pendingintent pit = pendingintent.getactivity (mcontext,<span class="hljs-number"><span class="hljs-number">0</span></span>, it,<span class="hljs-number"><span class="hljs-number">0</span></span>);<span class="hljs-comment"><span class="hljs-comment">//set picture, Notification title, send time, prompt method and other attributes</span></span>Notification.builder Mbuilder =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Notification.builder (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>); Mbuilder.setcontenttitle (<span class="hljs-string"><span class="hljs-string">"Ye Liangchen"</span></span>)<span class="hljs-comment"><span class="hljs-comment">//title</span></span>. Setcontenttext (<span class="hljs-string"><span class="hljs-string">"i have 100 ways to keep you from going."</span></span>)<span class="hljs-comment"><span class="hljs-comment">//content</span></span>. Setsubtext (<span class="hljs-string"><span class="hljs-string">"--remember My name Ye Liangchen."</span></span>)<span class="hljs-comment"><span class="hljs-comment">//content A small paragraph of text below</span></span>. Setticker (<span class="hljs-string"><span class="hljs-string">"received The information sent by Ye Liangchen ~"</span></span>)<span class="hljs-comment"><span class="hljs-comment">text message displayed in status bar after receiving information</span></span>. Setwhen (system.currenttimemillis ())<span class="hljs-comment"><span class="hljs-comment">//set Notification Time</span></span>. Setsmallicon (r.mipmap.ic_lol_icon)<span class="hljs-comment"><span class="hljs-comment">//set Small Icons</span></span>. Setlargeicon (largebitmap)<span class="hljs-comment"><span class="hljs-comment">//set Large Icons</span></span>. setdefaults (notification.default_lights | Notification.default_vibrate)<span class="hljs-comment"><span class="hljs-comment">//set default Tri-color lamp with vibrator</span></span>. Setsound (uri.parse (<span class="hljs-string"><span class="hljs-string">"android.resource://"</span></span>+ Getpackagename () +<span class="hljs-string"><span class="hljs-string">"/"</span></span>+ R.raw.biaobiao))<span class="hljs-comment"><span class="hljs-comment">//set a custom beep</span></span>. Setautocancel (<span class="hljs-keyword"><span class="hljs-keyword">true</span></span>)<span class="hljs-comment"><span class="hljs-comment">//set Click to cancel notification</span></span>. setcontentintent (pit);<span class="hljs-comment"><span class="hljs-comment">//set pendingintent</span></span>Notify1 = Mbuilder.build (); Mnmanager.notify (notifyid_1, notify1);<span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>;<span class="hljs-keyword"><span class="hljs-keyword"></span> case</span>R.id.btn_close_normal:<span class="hljs-comment"><span class="hljs-comment">//in addition to the ability to cancel notification by id, You can also call Cancelall (); close all notifications generated by the app</span></span>Mnmanager.cancel (notifyid_1);<span class="hljs-comment"><span class="hljs-comment">//cancel Notification</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>; } }}</code></pre><p><p>The comment is very detailed, does not have to say each very carefully ~</p></p>5. Code Sample Download:<p><p>Notificationdemo.zip</p></p>This section summarizes: <blockquote> <blockquote> <p>ok, This section introduces you to the basic usage of notification in the 4.x version, very simple is it ~<br>of course, You can also customize notification interested can check the relevant information on their own, here is not slowly<br>Study ~ yes, part of this section of the following blog, paste the link, we can also go to see:<br>ANDROID Notification bar Notification integrated Learning (a demo lets you fully understand It)<br>It's written in detail ~ This section is here, thank you ~</p> </blockquote> </blockquote> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android Basics Getting Started tutorial--2.5.2 Notification (status bar Notification) Detailed</p></p></span>

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.