Android: Use of notification bar

Source: Internet
Author: User

It's been a long time since I used the Android notification feature, and today I moved the code two years ago to see it. Found that a lot of methods are discarded, the code of various strikethrough look very uncomfortable. So, open Google and view official documents. Learn the latest ways to send notification bar messages.

The code in this article is written in Google's official documentation:

Http://developer.android.com/guide/topics/ui/notifiers/notifications.html

1. First. To obtain Notification Services for the system:

Notificationmanager nm = (Notificationmanager) getsystemservice (Context.notification_service);
2. Send one of the simplest notifications

public void Simplenotice (view view) {//This builder is in Android.support.v4.app.NotificationCompat.Builder, same as below. Builder Mbuilder = new Builder (this);        When the system receives a notification. The text displayed above the notification bar.

Mbuilder.setticker ("Tianjin. Sunny, 2~15度, Breeze "); Small icon Mbuilder.setsmallicon (r.drawable.consult_answer) displayed on the notification bar; Notification title mbuilder.setcontenttitle ("Weather Forecast"); Notice content Mbuilder.setcontenttext ("Tianjin, Sunny, 2~15度. Breeze "); Set the large icon, which is the picture on the left side of the notification bar (assuming a small icon is only set, the small icon is shown here) Mbuilder.setlargeicon (Bitmapfactory.decoderesource (Getresources () , R.drawable.share_sina)); Digital Mbuilder.setnumber (6) shown on the left side of the small icon; Set to non-purge mode mbuilder.setongoing (true); Displays the notification, the ID must not be repeated, otherwise the new notification overwrites the old notification (taking advantage of this feature. Ability to update notifications) nm.notify (1, Mbuilder.build ()); }

3. Delete a notification. The number of references is the ID of the notification
Nm.cancel (1);
4. Send a notification. Click on the notification to jump to an activity, from the activity to return to a page within the program (usually the homepage)

Click on the notification to enter an activity and click Back to enter the specified page.        public void Resultactivitybackapp (view view) {Builder Mbuilder = new Builder (this);        Mbuilder.setticker ("notice title 2");        Mbuilder.setsmallicon (R.drawable.ic_launcher);        Mbuilder.setcontenttitle ("notice title 2"); Mbuilder.setcontenttext ("Click on the notification to enter an activity, click Back to enter the specified page.")

"); Set the Click once to disappear (assuming no click event. The method is not valid.

) Mbuilder.setautocancel (TRUE); Click on the notification to jump after the page Intent resultintent = new Intent (this, resultactivitybackapp.class); Use Taskstackbuilder to set the return relationship for the notification page Taskstackbuilder Stackbuilder = taskstackbuilder.create (this); Returns the page setting for the page that opens after clicking the notification. (specified in manifest) stackbuilder.addparentstack (Resultactivitybackapp.class); Stackbuilder.addnextintent (resultintent); Pendingintent pintent = stackbuilder.getpendingintent (0, pendingintent.flag_update_current); Mbuilder.setcontentintent (pintent); MId allows to update the notification later on. Nm.notify (2, Mbuilder.build ()); }

At the same time. You need to specify the parent activity in manifest for activity that opens after clicking the notification.

<activity            android:name= ". Resultactivitybackapp "            android:parentactivityname=". Mainactivity ">            <meta-data                android:name=" Android.support.PARENT_ACTIVITY "                android:value=". Mainactivity "/>        </activity>
(The property of activity is parentactivityname as an attribute in API 16.) The code in Meta-data is compatible with API 16 below.

Therefore, for most programs. All two of these places have to be written. )

5. Similar to the above 4, just go back to the home page when you return to the open activity

Click on the notification to enter an activity, click Return to Desktop public    void Resultactivitybackhome (view view) {        Builder mbuilder = new Builder ( this);        Mbuilder.setticker ("notice title 3");        Mbuilder.setsmallicon (r.drawable.ic_launcher);        Mbuilder.setcontenttitle ("notice title 3");        Mbuilder.setcontenttext ("Click on the notification to enter an activity, click Back to the Desktop");        Set the Click once to disappear (assuming there is no click event, the method is not valid.) )        Mbuilder.setautocancel (true);        Intent notifyintent = new Intent (this, resultactivitybackhome.class);        Notifyintent.setflags (intent.flag_activity_new_task);        Pendingintent pintent = pendingintent.getactivity (this, 0, notifyintent, pendingintent.flag_update_current);        Mbuilder.setcontentintent (pintent);        Nm.notify (3, Mbuilder.build ());    }
6. Notification with progress bar

     public void Progressnotice (view view) {Final builder Mbuilder = new Builder (this);        Mbuilder.setticker ("notice title 4"); Mbuilder.setcontenttitle ("Picture Download"). Setcontenttext ("Download in Progress"). Setsma        Llicon (R.drawable.ic_launcher); Start a lengthy operation in a background thread new thread (new Runnable () {@Override Pub                LIC void Run () {int progress;  for (progress = 0; Progress <=. progress++) {//sets the progress indicator to a max value, the  Current completion percentage,//and "determinate" state mbuilder.setprogress (100,                    Progress, false);                    Do not understand progress bar//mbuilder.setprogress (0, 0, true);                    Nm.notify (4, Mbuilder.build ());         Analog delay try {thread.sleep (200);           } catch (Interruptedexception e) {e.printstacktrace (); }}//When the loop is finished, updates the notification Mbuilder.setconten                Ttext ("Download complete");                Removes the progress bar mbuilder.setprogress (0, 0, false);            Nm.notify (4, Mbuilder.build ());    }}). Start (); }
7. Notification of extended layouts. Slide down the notification bar to see more specific content

public void Expandlayoutnotice (view view) {        Builder mbuilder = new Builder (this);        Mbuilder.setticker ("notice title 5");        Mbuilder.setsmallicon (r.drawable.ic_launcher);        Mbuilder.setcontenttitle ("notice title 5");        Mbuilder.setcontenttext ("Press and hold the notification drop-down to show the extended layout");        Notificationcompat.inboxstyle Inboxstyle = new Notificationcompat.inboxstyle ();        String[] Events = new string[]{"Beijing", "Tianjin", "Shanghai", "Guangzhou"};        Sets the title of the extended layout        inboxstyle.setbigcontenttitle ("Event Tracker Details:");        for (String s:events) {            inboxstyle.addline (s);        }        Mbuilder.setstyle (Inboxstyle);        Nm.notify (5, Mbuilder.build ());    }

8. Define the notification bar for your layout. (This is not recommended under Google's official documentation.) As a result of this approach, there are too many factors to consider when adapting to different screens.

Also, the notification bar should show the most concise information. The default layout for most programs is sufficient.

)

Notice that you define the layout public void Customlayoutnotice (view view) {Builder Mbuilder = new Builder (this);        Mbuilder.setticker ("notice title 6");        Mbuilder.setticker ("notice title 6");        Mbuilder.setsmallicon (R.drawable.ic_launcher);        Remoteviews remoteviews = new Remoteviews (Getpackagename (), r.layout.custom_layout_notice);        Mbuilder.setcontent (remoteviews);        Set the text remoteviews.setcharsequence (R.id.custom_layout_button1, "SetText", "Button1") for the buttons on the remoteviews;        Remoteviews.setcharsequence (R.id.custom_layout_button2, "SetText", "Button2");        Set the Click event for the button on the remoteviews Intent intent1 = new Intent (this, customlayoutresultactivity.class);        Intent1.putextra ("Content", "from Button1 click!");        Pendingintent PIntentButton1 = pendingintent.getactivity (this, 0, intent1, pendingintent.flag_update_current);        Remoteviews.setonclickpendingintent (R.id.custom_layout_button1, PIntentButton1); Intent Intent2 = new Intent (This, customlayoutreSultactivity.class);        Intent2.putextra ("Content", "from Button2 click!");        Pendingintent PIntentButton2 = Pendingintent.getactivity (this, 1, Intent2, pendingintent.flag_update_current);        Remoteviews.setonclickpendingintent (R.id.custom_layout_button2, PIntentButton2);    Nm.notify (6, Mbuilder.build ()); }


Android: Use of notification bar

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.