Basic Android tutorial-how to use the notification (notificationmanager) in the android status bar!

Source: Internet
Author: User

Hello everyone, let's briefly talk about the android status bar reminder, which will also be used frequently during development. When we plug in USB, there will be a status bar reminder, and a text message will also contain a status bar reminder.

Alertdialog can also be used if there is a reminder function in Android, but we need to review the usage, because when using alertdialog, the user's ongoing operations will be interrupted.

Because the current focus is obtained by alertdialog. We can imagine that a text message was sent when a user played a game well. If alertdialog is used as a reminder, you must handle this reminder before continuing the game. The user may be angry. However, using notification will not cause these troubles. Users can play the game and then read this text message. Therefore, select appropriate controls based on actual needs during development.

Now, I wrote a demo to teach you how to use notification in the following steps:

 

Step 1: Create an android project named icationicationdemo.

 

Step 2: Modify the main. XML Code as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "welcome to Mr Wei's blog" <br/> <button <br/> Android: id = "@ + ID/showbutton" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "shownotification" <br/> <button <br/> Android: Id = "@ + ID/cancelbutton" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "cancelnotification" <br/> </linearlayout> <br/>

 

Step 3: Modify icationicationdemo. Java code as follows:

Package COM. tutor. notification; <br/> Import android. app. activity; <br/> Import android. app. notification; <br/> Import android. app. icationicationmanager; <br/> Import android. app. pendingintent; <br/> Import android. content. context; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. butt On; <br/> public class icationicationdemo extends activity implements onclicklistener {</P> <p> private context mcontext; <br/> private button showbutton, cancelbutton; <br/> private notification mnotification; <br/> private notificationmanager micationicationmanager; <br/> private final static int notification_id = 0x0001; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> supe R. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> setupviews (); <br/>}< br/> // here are some initialization operations. You can see oncreate () the code in the method is very concise. <Br/> Public void setupviews () {<br/> mcontext = icationicationdemo. this; <br/> showbutton = (button) findviewbyid (R. id. showbutton); <br/> cancelbutton = (button) findviewbyid (R. id. cancelbutton); </P> <p> mnotification = new notification (R. drawable. icon, "This is a notification. ", system. currenttimemillis (); <br/> // The default sound will be used to remind the user <br/> mnotification. defaults = notification. default_sound; <br/> mnotificat Ionmanager = (icationicationmanager) This. getsystemservice (icationication_service); </P> <p> showbutton. setonclicklistener (this); <br/> cancelbutton. setonclicklistener (this); <br/>}< br/> // click the Event Response button <br/> Public void onclick (view V) {</P> <p> If (V = showbutton) {<br/> intent mintent = new intent (mcontext, icationicationdemo. class); <br/> // set intent here. flag_activity_new_task attribute <br/> mintent. setflags (intent. flag _ Activity_new_task); <br/> pendingintent mcontentintent = pendingintent. getactivity (mcontext, 0, mintent, 0); <br/> // you must use setlatesteventinfo (context, title, content, pendingintent). Otherwise, an error is returned. <br/> mnotification. setlatesteventinfo (mcontext, "10086", "your current bill is insufficient. Please recharge your account. haha ~ ", Mcontentintent); <br/> // send a notification here (Message ID, notification object) <br/> micationicationmanager. Y (ication_id _id, mnotification); <br/>} else if (V = cancelbutton) {<br/> // if the notification ID is canceled, it is OK. <br/> micationicationmanager. cancel (icationication_id); <br/>}< br/>}

Step 4: run the android project, as shown in the following figure:

 

 

 

OK. Let's write it here today. Good night everyone ~ If you have any questions, please leave a message. THX ~

 

 

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.