Android: Use of notification bar

Source: Internet
Author: User

For a long time without using the Android notification feature, today, two years ago the code moved out a look, found a lot of methods are discarded, the code of various strikethrough look very uncomfortable. So, open Google, view official documents, and learn the latest ways to send notification bar messages.

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

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

1. First, get the notification service 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);        The text displayed above the notification bar when the system receives a notification.        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 a large icon, which is the picture on the left side of the notification bar (small icons are shown here if only small icons are set)        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 duplicated, otherwise the new notification overwrites the old notification (with this feature, the notification can be updated)        nm.notify (1, Mbuilder.build ());    }
3. Delete a notification. parameter 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 returned, into the program of a page (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 (if there is no click event, the method is invalid.)        ) 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 ()); }
Also, you need to specify the parent activity in manifest for the activity that is opened after clicking the notification.

<activity            android:name= ". Resultactivitybackapp "            android:parentactivityname=". Mainactivity ">            <meta-data                android:name=" Android.support.PARENT_ACTIVITY "                android:value=". Mainactivity "/>        </activity>
(where the Activity property is parentactivityname as an attribute in API 16, the code in Meta-data is below compatible with API 16.) Therefore, for most programs, these two 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 (if there is no click event, the method is invalid.) )        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);                    Progress bar with unclear progress//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. Press and hold the notification strip to see more details

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. Customize the layout of the notification bar. (This is not recommended according to Google's official documentation, as there are too many factors to consider when using this approach for different screens.) Moreover, the notification bar should show the most concise information, for most programs the default layout is sufficient. )

Notification of a custom 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

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.