The use of notification in Android

Source: Internet
Author: User
<span id="Label3"></p><p><p>Notification in the use of mobile phones are very common, for example, we receive a text message, in our notification bar will display an icon to remind us, which we can use notification to Achieve. He has a lot of usage, such as a hint of a similar message, a progress bar hint, a folding type, or a hanging type. Below we can see a simple and basic notification:</p></p><p><p>The first type: basic notification</p></p><p><p>1.API 11 or less, is now deprecated, and its simple usage is this</p></p><p><p>1.1 Need to use pendingintent here, similar to Intent, Intent is in time to start, Intent with the activity disappears, and pendingintent it is not immediately called, it is mainly used for the upcoming events, In notification, the activity jumps only when the Drop-down status bar is Clicked.</p></p><pre><pre>Pendingintent pendingintent = pendingintent.getactivity (<span style="color: #0000ff;"></span>this<span style="color: #800080;">0</span>, <span style="color: #0000ff;">new</span> Intent<span style="color: #0000ff;"> </span>, Mainactivity. <span style="color: #0000ff;">class</span> <span style="color: #800080;">0</span>); </pre></pre><p><p>1.2 and then we're looking at how notification is Used.</p></p><pre>Notification notify =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Notification (); Notify.icon</span></span>=<span style="color: #000000;"><span style="color: #000000;">r.drawable.ic_launcher; Notify.tickertext</span></span>=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">There's a new message! </span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">; Notify.when</span></span>=<span style="color: #000000;"><span style="color: #000000;">System.currenttimemillis (); <strong><span style="color: #0000ff;">Notify.setlatesteventinfo (</span></strong> </span></span><strong>this <strong><span style="color: #0000ff;">, "notification message title", is basically not found in later versions, or prompts for "message content" is not recommended</span></strong></strong><span style="color: #000000;"><span style="color: #000000;"> <strong><span style="color: #0000ff;">, pendingintent); </span></strong>Notify.number</span></span>=<span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;">; Number of messages Notify.flags</span></span>|= notification.flag_auto_cancel;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Flag_auto_cancel indicates that the notification will be cleared when the notification is clicked by the User.<br><br></span></span></pre><p><p>Simply mention how the 2.x version is handled:</p></p><pre><pre><span style="color: #0000ff;">New</span> <span style="color: #000000;">Notification (r.drawable.message,</span><span style="color: #008000;"></span></pre></pre><p><p>1.3 Then we need to use the System's Notification Manager to initiate <span style="line-height: 1.5;"></span> notifications</p></p><pre><pre>Notificationmanager manager =<span style="color: #000000;"> (notificationmanager) getsystemservice (context.notification_service); Manager.notify (notification_flag, notify); Initiating a notification</span></pre></pre><p><p>Let's see: is it a familiar feeling?</p></p><p><p></p></p><p><p>2.api11 after the main is to create notifications through notification.builder, we look at the code implementation</p></p><pre><pre>Pendingintent pendingintent = pendingintent.getactivity (this, 0, new Intent (this, mainactivity.class), 0);<br><br><span style="color: #0000ff;">New</span> Notification.builder (<span style="color: #0000ff;"></span>this<span>)</span></pre></pre><pre><span style="color: #000000;"><span style="color: #000000;">. Setsmallicon (r.mipmap.ic_launcher)</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set the small picture in the status bar, the size is generally recommended in 24x24, where you can also set a large icon</span></span>. Setticker (<span style="color: #800000;"><span style="color: #800000;">There's a new message! </span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>)<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Set</span></span><span style="color: #008000;"><span style="color: #008000;">prompt Text to display</span></span>. Setcontenttitle (<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Title</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>)<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Set</span></span><span style="color: #008000;"><span style="color: #008000;">the caption that is displayed</span></span>. Setcontenttext (<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;">This is <span style="color: #800000;">message content</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>)<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">details of the message</span></span>. setcontentintent (pendingintent)<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Association Pendingintent</span></span>. Setnumber (<span style="color: #800080;"><span style="color: #800080;">1</span></span>)<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">number shown on the right of TextView <span style="color: #0000ff;">can be defined externally by a variable, click on the cumulative Setnumber (count), and</span> </span> the</span>. getnotification ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">be <span style="color: #008000;">aware that build () is at the API</span> level</span><span style="color: #008000;"><span style="color: #008000;">16 and later added, in API11 can use Getnotificatin () to replace</span></span>Notify.flags |=<span style="color: #000000;"><span style="color: #000000;">notification.flag_auto_cancel; Notificationmanager Manager</span></span>=<span style="color: #000000;"><span style="color: #000000;">(notificationmanager) Getsystemservice (context.notification_service); Manager.notify (notification_flag, notify);</span></span></pre><p><p>Overall there is no change, and we can also set some of the following properties:</p></p><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">setting the priority level</span> <span style="color: #000000;"> notify.setpriority (notification.priority_default); </span> <span style="color: #008000;">//</span> <span style="color: #008000;">set up notification categories</span> Notify.setcategory (notification.category_message);</pre></pre><p><p>3. We can also use the custom notification: the main use of remoteviews,</p></p><pre><pre>the <span style="font-size: 15px;">First Step: Write what you want.</span> <span style="font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 1.5;">notification-style XML file <span style="font-size: 15px;"></span><br>Step two: Add the following code to the above sake:<br></span></pre></pre><pre><pre><span style="color: #0000ff;">New</span> <span style="color: #000000;">remoteviews (getpackagename (), r.layout.my_notification); </span> <span style="color: #800000;">"</span> <span style="color: #800000;">hello!</span> <span style="color: #800000;">"</span> <span style="color: #000000;">); </span></pre></pre><p><p>You can use it to do a collapsed view, briefly speaking, it is mainly used to display long text, it has 2 views, one is normal state, one is the expanded state,</p></p><pre>Intent Intent =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Intent (intent.action_main); Pendingintent contentintent</span></span>= Pendingintent.getactivity (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>,<span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;">, intent,</span></span><span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">); Notification.builder Buider</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Notification.builder (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">). setsmallicon (r.mipmap.ic_launcher). setticker (</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">you have a new short message</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">). Setcontenttitle (</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Notification Title</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">). Setcontenttext (</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;">This is <span style="color: #800000;">message</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">). setcontentintent (contentintent). setautocancel (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">). setlargeicon (bitmapfactory.decoderesource (getresources (), r.mipmap.ic_launcher)); Remoteviews Contentview</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">remoteviews (getpackagename (), r.layout.my_notification); Contentview.settextviewtext (r.id.text_content,</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">show!</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">); Notification Notification1</span></span>=<span style="color: #000000;"><span style="color: #000000;">Buider.build (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">specify view notification in normal state</span></span>notification1.contentview=<span style="color: #000000;"><span style="color: #000000;">contentview; Remoteviews Expandview</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">remoteviews (getpackagename (), r.layout.my_notification_expand);<br> <span style="color: #339966;">//expand View</span> notification1.bigcontentview</span></span>=<span style="color: #000000;"><span style="color: #000000;">expandview; Manager.notify (notification_flag, notification1);</span></span></pre><p><p>4. Hanging Type notification, He is 5.0 in the new, that is, the API HeadsUp notification, can son ah do not interrupt the user operation, to inform the user</p></p><p><p>It differs from the other, mainly in the following code:</p></p><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">set to hang, main settings setfullscreenintent</span> Notify.setcontenttext (<span style="color: #800000;">"</span><span style="color: #800000;">heads-up,notification on5.0</span><span style="color: #800000;">"</span>). <strong><span style="color: #0000ff;">setfullscreenintent</span></strong> <span style="color: #0000ff;">true</span>);</pre></pre><p><p>Such as:</p></p><p><p></p></p><p><p>5. Progress bar of the notification, we generally common is the download software, the display will have a download progress notification, in fact, This is also very good implementation of the '</p></p><p><p>This is described in the Api:</p></p><p><p>To use a progress indicator on platforms starting with Android 4.0, call <code>setProgress()</code> . For previous versions, You must create your own custom notification layout that includes a <code>ProgressBar</code> view.</p></p><p><p>It means that in android4.0 above the platform, we can directly setprogress () method, But for the previous we can only customize the Layout.</p></p><p><p>Let's take a brief look: this is a demo of the Api. <span style="color: #ff0000;">let's focus on the coloring section below</span></p></p><pre>Mnotificationmanager =<span style="color: #000000;"><span style="color: #000000;">(notificationmanager) Getsystemservice (context.notification_service); Builder</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Notificationcompat.builder (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); Builder.setcontenttitle (</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;">picture <span style="color: #800000;">Download</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">). Setcontenttext (</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Download in Progress</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">). Setsmallicon (r.mipmap.ic_launcher); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Thread (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Runnable () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Run () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">incr; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">do <span style="color: #008000;">the "lengthy" operation</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(INCR =<span style="color: #800080;"><span style="color: #800080;">0</span></span>; INCR <=<span style="color: #800080;"><span style="color: #800080;"></span> -</span>; incr+=<span style="color: #800080;"><span style="color: #800080;">5</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">sets the progress indicator to a max value, the</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">current <span style="color: #008000;">completion percentage, and "determinate"</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;"></span> state</span>Builder.setprogress (<span style="color: #800080;"><span style="color: #800080;"></span> -</span>, incr,<span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Displays the progress bar</span> for the first Time.</span>Mnotificationmanager.notify (<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">, Builder.build ()); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">sleeps the thread, simulating an operation</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">that <span style="color: #008000;">takes</span> time</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Sleep for 5 seconds</span></span>Thread.Sleep (<span style="color: #800080;"><span style="color: #800080;">5</span></span>*<span style="color: #800080;"><span style="color: #800080;"></span> +</span><span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(interruptedexception e) {log.d (TAG,</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Sleep Failure</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">); } } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">when <span style="color: #008000;">the loop is finished, updates the notification</span></span> <strong><strong><span style="color: #0000ff;">Builder.setcontenttext ("Download complete")//removes the progress bar . setprogress (0,0,true</span></strong></strong><span style="color: #000000;"><span style="color: #000000;"><strong><span style="color: #0000ff;">);</span></strong> Mnotificationmanager.notify (notification_flag, Builder.build ()); } }</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">starts the thread by calling the run () method</span> in its Runnable</span>). Start ();</pre><pre class="prettyprint"><span style="color: #ff0000;"><span style="color: #ff0000;"><span class="pln">mbuilder<span class="pun">.<span class="pun" setprogress<span> (<span class="lit">100<span class="pun">,<span class="pln"> Incr<span class="pun"> , <span class="pln"> <span class="kwd">false<span class="pun" , and < span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> <em style="line-height:1.5;"><span class="pln">mbuilder<span class="pun">.< Span class= "pln" >setprogress<span class="pun"> (<span class="lit">0<span class="pun">,<span class="pln"> <span class="lit">0<span class="pun">,<span class="pln"> <span class="kwd">true<span class="pun">), If the blue part is replaced with the former, At runtime, we will see that it will give us a notification when the download is complete, but the latter will give us a hint that the download is complete while the progress bar is running, continuous and scrolling, that is, the effect is different <br> <span style="color: #000000;"> Example: The first figure is when the download is in progress, and the second one is I use the </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></em></span></span><span class="pln"><span class="pln"><span class="pun"><span class="pln"><span class="pun"><span class="lit"><span class="pun"><span class="pln"><span class="pun"><span class="pln"><span class="kwd"><span class="pun"> <span class="pln">mbuilder <span class="pln" . <span>setprogress<span class="pun"> (<span class="lit">0<span class="pun">,<span class="pln"> <span class="lit">0<span class="pun">,<span class="pln"> <span class="kwd">true<span class="pun"> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span> </span></span></span></span></span><em id="__mceDel" style="line-height: 1.5;"><em id="__mcedel" style="line-height:1.5;"><span style="color: #ff0000;"><em id="__mcedel" style="line-height:1.5;">< Span class= "pln" ><span class="pun"><span class="pln"><span class="pun"><span class="lit"><span class="pun">< Span class= "pln" ><span class="lit"><span class="pun"><span class="pln"><span class="kwd"><span class="pun">< Span style= "color: #000000;" >, when the download is complete, we can find that he still has a progress bar, but has been rolling, I here is a static picture, you can run a look at the effect, the third figure, mbuilder.setprogress (100,incr,false), the download completed when the notification displayed </span></span></span></span></span></span></span></span></span></span></em></span></em></em></pre><pre class="prettyprint"><pre class="prettyprint"><em id="__mceDel"><span style="color: #ff0000;"><em id="__mceDel" style="line-height: 1.5;"><span class="pln"><span class="pun"><span class="pln"><span class="pun"><span class="lit"><span class="pun"><span class="pln"><span class="lit"><span class="pun"><span class="pln"><span class="kwd"><span class="pun"><br><br><br><span style="color: #000000;">There are some other, such as the lock screen, display level of notification, here I do not write, we can look at the Api. The introduction is very detailed.</span><br></span></span></span></span></span></span></span></span></span></span></span></span></em></span></em></pre></pre><p><p></p></p><p><p>The use of notification in Android</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.