android - How to close the notification dialog after click the dismiss button ,without calling any activity -
image have code. working fine , close notification. when click on "dismiss" button, open application , close dialog. not required me. want, notification dialog close after click on "dismiss" button without opening activity.
mainactivity.java
public class mainactivity extends activity{ static int nummessages = 0; context context; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.notificationmain); context = getapplicationcontext(); button bcustomnotifyaction = (button) findviewbyid(r.id.customnotificationaction); bcustomnotifyaction.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { customnotificationaction(); } }); } @targetapi(build.version_codes.honeycomb) private void customnotificationaction() { // set notification title string strtitle = "you got new notification."; // set notification text string strtext ="hi,how you?"; // open notificationview class on notification click intent intent = new intent(this, notificationview.class); // send data notificationview class intent.putextra("title", strtitle); intent.putextra("text", strtext); // open notificationview.java activity pendingintent pintent = pendingintent.getactivity(this, 0, intent, pendingintent.flag_update_current); int notificationid = new random().nextint(); // use counter in util class... pendingintent dismissintent = notificationactivity.getdismissintent(notificationid, getapplicationcontext()); //create notification using notificationcompat.builder notificationcompat.builder builder = new notificationcompat.builder(this) // set icon .setsmallicon(r.drawable.logosmall) // set ticker message .setticker(strtitle) // set title .setcontenttitle(strtitle) // set text .setcontenttext(strtext) // add action button below notification .addaction(android.r.drawable.ic_menu_close_clear_cancel, "dismiss", dismissintent) .addaction(android.r.drawable.ic_menu_info_details, "info", pintent) // set pendingintent notification .setcontentintent(pintent) // showing action button on notification .setpriority(notification.priority_max) .setwhen(0) /* increase notification number every time new notification arrives */ .setnumber(++nummessages) // dismiss notification .setautocancel(true); // create notification manager notificationmanager notificationmanager = (notificationmanager) context.getsystemservice(context.notification_service); // build notification notification manager notificationmanager.notify(0, builder.build()); } }
notificationmain.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".notificationview" > <button android:id="@+id/customnotificationaction" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:text="@string/customnotificationaction" /> </relativelayout>
notificationview.java
public class notificationview extends activity { string title; string text; textview txttitle; textview txttext; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.notificationview); // create notification manager notificationmanager notificationmanager = (notificationmanager) getsystemservice(notification_service); // dismiss notification notificationmanager.cancel(0); // retrive data mainactivity.java intent = getintent(); title = i.getstringextra("title"); text = i.getstringextra("text"); // locate textview txttitle = (textview) findviewbyid(r.id.title); txttext = (textview) findviewbyid(r.id.text); // set data textview txttitle.settext(title); txttext.settext(text); } }
notificationview.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <textview android:id="@+id/lbltitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/lbltitle" /> <textview android:id="@+id/lbltext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/lbltitle" android:text="@string/lbltext" /> <textview android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_torightof="@+id/lbltitle" /> <textview android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/title" android:layout_torightof="@+id/lbltext" /> </relativelayout>
notificationactivity.java
public class notificationactivity extends activity { public static final string notification_id = "notification_id"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); notificationmanager manager = (notificationmanager) getsystemservice(notification_service); manager.cancel(0); finish(); // since finish() called in oncreate(), ondestroy() called } public static pendingintent getdismissintent(int notificationid, context context) { intent intent = new intent(context, notificationactivity.class); intent.setflags(intent.flag_activity_clear_top | intent.flag_activity_single_top); intent.setflags(intent.flag_activity_new_task | intent.flag_activity_clear_task); intent.putextra(notification_id, notificationid); pendingintent dismissintent = pendingintent.getactivity(context, 0, intent, pendingintent.flag_cancel_current); return dismissintent; } }
solution:
i got solution using broadcast receiver .
code changed : added code in mainactivity.java customnotificationaction() .
//create intent broadcastreceiver intent buttonintent = new intent(context, buttonreceiver.class); //create pendingintent pendingintent btpendingintent = pendingintent.getbroadcast(context, 0, buttonintent,0);
and changed line from
.addaction(android.r.drawable.ic_menu_close_clear_cancel, "dismiss", dismissintent)
to
.addaction(android.r.drawable.ic_menu_close_clear_cancel, "dismiss", btpendingintent)
and added 1 java file
buttonreceiver.java
public class buttonreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { toast.maketext(context, "notification dialog closed", toast.length_long).show(); log.d("notification:","notification dialog closed"); notificationmanager manager = (notificationmanager) context.getsystemservice(context.notification_service); manager.cancel(0); pendingintent resultpendingintent = pendingintent.getactivity(context, 0, new intent(), 0); notificationcompat.builder mb = new notificationcompat.builder(context); mb.setcontentintent(resultpendingintent); } }
and register broadcast in mainfest file
<receiver android:name=".buttonreceiver"/>
and problem solved :)
you can download source code link :
should set broadcast receiver detect action click without starting activity.
dismiss ongoing android notification via action button without opening app
Comments
Post a Comment