Android Notification and Taskstackbuilder

Source: Internet
Author: User

In app development, notification bar notifications are a more common requirement, and there's an experience detail here. Pop-up notice, the user drop-down notification bar, click to enter the app in the corresponding page, after reading to return to the previous page, then click on the upper left corner or the upper right corner of the back button (generally should have), if the user is from the desktop click Notification into the app, then there is no corresponding page in the app stack, It is not possible to return to the previous page without the usual destruction of the current page, which requires the use of taskstackbuilder.

Taskstackbuilder, my understanding is a stack builder, in the XML to configure Activity1 a parentactivityname=activity2, Then through the Taskstackbuilder start Activity1, the system will first press the Activity2 into the stack, and then start activity1, then if the destruction of Activity1,activity2 will become the top page rendered in front of the user. This simulates the historical page effect, the experience is better than the user clicks back directly back to the desktop.

public class Notificationutil {/** * Display notification */public static void Shownotifiation (context context, String tit        Le, String content, int nid, Pushreceivermodel pushmodel) {staticmethod.debugemsg ("Send notification to notification bar"); Notificationcompat.builder Mbuilder = new Notificationcompat.builder (context). SETSM        Allicon (R.drawable.push). Setcontenttitle (title). Setcontenttext (content);        Taskstackbuilder Stackbuilder = taskstackbuilder.create (context);        Intent resultintent; if (Staticvariable.rid! = NULL &&!) StaticVariable.rid.equals ("")) {if (StaticVariable.rid.equals ("3") | |                    StaticVariable.rid.equals ("5")) {if (Pushmodel.getmessagetype (). Equals (staticconstant. System notification)) {                    Resultintent = new Intent (context, xsjl_sysytemmsgactivity.class);                Stackbuilder.addparentstack (Xsjl_sysytemmsgactivity.class); }else{resultintent = new Intent (context, xsjl_msgboxactivity.class);                Stackbuilder.addparentstack (Xsjl_msgboxactivity.class); }} else if (StaticVariable.rid.equals ("6") | |                    StaticVariable.rid.equals ("7")) {if (Pushmodel.getmessagetype (). Equals (staticconstant. Dialog)) {                    Resultintent = new Intent (context, wechat_2_activity.class);                    Resultintent.putextra ("CID", Pushmodel.getuid ());                    Resultintent.putextra ("title", Pushmodel.getname ());                Stackbuilder.addparentstack (Wechat_2_activity.class); } else if (Pushmodel.getmessagetype (). Equals (staticconstant. Sales new user)) {resultintent = Intent (context                    , Gjxq_1_activity.class);                     if (Staticmethod.isnotnull (Pushmodel.getuid ())) {Resultintent.putextra ("cid", Pushmodel.getuid ()); } else {ResultIntEnt.putextra ("CID", Pushmodel.getadvid ());                } stackbuilder.addparentstack (Gjxq_1_activity.class);  } else if (Pushmodel.getmessagetype (). Equals (staticconstant. System notification)) {resultintent = new Intent (context,                    Xsgw_sysytemmsgactivity.class);                Stackbuilder.addparentstack (Xsgw_sysytemmsgactivity.class); } else if (Pushmodel.getmessagetype () equals (staticconstant. Rob client)) {resultintent = new Intent (context,                Mainactivity.class);                    } else {resultintent = new Intent (context, xsgw_tasknotifyactivity.class);                Stackbuilder.addparentstack (Xsgw_tasknotifyactivity.class);            }} else {resultintent = new Intent (context, launchactivity.class);        }} else {resultintent = new Intent (context, launchactivity.class);  } if (Stackbuilder.getintentcount () > 0) {          Stackbuilder.editintentat (0). PutExtra ("aid", Staticvariable.aid);        Stackbuilder.editintentat (0). PutExtra ("UID", staticvariable.uid);        } stackbuilder.addnextintent (Resultintent); Pendingintent resultpendingintent = stackbuilder.getpendingintent (0, Pendingintent.flag_update_current |        Intent.flag_activity_new_task);        Mbuilder.setcontentintent (resultpendingintent); Notificationmanager Mnotificationmanager = (notificationmanager) context.getsystemservice (Context.NOTIFICAT        Ion_service);        Notification Notification = Mbuilder.build ();        Notification.flags = Notification.flag_auto_cancel; Notification.defaults = Notification.default_sound |        Notification.default_vibrate;        Long[] Vibrate = {0, 100, 200, 300};        Notification.vibrate = vibrate;    Mnotificationmanager.notify (NID, Notification); }}

There is a relatively easy to confuse the place, is parentactivity. Here is an example with xsjl_sysytemmsgactivity.

Here's a statement from Xsjl_systemmsgactivity in Androidmanifest.xml.

<activity            android:name= ". Page.base.xsjl.activity.XSJL_SysytemMsgActivity"            android: Parentactivityname= ". Page.base.xsjl.activity.MainActivity"/>

PARENTACTIVITYNAME Specifies a history page that needs to be displayed when Xsjl_systemmsgactivity is destroyed.

It's written in the code.

Resultintent = new Intent (context, xsjl_sysytemmsgactivity.class);//resultintent.putextra ("Cid", Pushmodel.getuid () ); Pass the start parameter to the xsjl_sysytemmsgactivity page//stackbuilder.editintentat (0). PutExtra ("aid", Staticvariable.aid); Pass the start parameter to the parent page, Stackbuilder.addparentstack (Xsjl_sysytemmsgactivity.class);

  

The key part is this, and the rest is written on the official website example.

Stackbuilder.addnextintent (resultintent);
Pendingintent resultpendingintent = stackbuilder.getpendingintent (0, Pendingintent.flag_update_current | Intent.flag_activity_new_task);

Android Notification and Taskstackbuilder

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.