Android Notification Monitor system clears the notification bar method

Source: Internet
Author: User

Write a notification notification tool class, which contains the listener system to clear the notification bar method, focus on bold italic part:

public class Notifier {
private static final String TAG = Notifier.class.getSimpleName ();
private static Notifier instance;
Private Context Mcontext;


private static final int notification_id_1 = 0;
private static final int notification_id_2 = 1;
private static final int notification_id_3 = 2;
private static final int notification_id_4 = 3;
private static final int notification_id_5 = 4;
private static final int notification_id_6 = 5;
private static final int notification_id_7 = 6;
private static final int notification_id_8 = 7;


public static Notifier getinstance (context context) {
if (instance = = null) {
Instance = new Notifier (context);
}
return instance;
}


Public Notifier (Context context) {
Mcontext = context;
}


Public Notificationmanager Getnotificationmanager (context context) {
Return (Notificationmanager) context
. Getsystemservice (Context.notification_service);
}


public void shownotify (context context, int largeicon, int smallicon,
Charsequence Tickertext, Charsequence ContentInfo,
Charsequence Contenttitle, charsequence contenttext) {


Pendingintent contentintent = pendingintent.getactivity (context, 0,
New Intent (context, Mainactivity.class), 0);


Bitmap Bitmap = Bitmapfactory.decoderesource (Context.getresources (),
LargeIcon);
Notificationcompat.builder Notifybuilder = new Notificationcompat.builder (
context);
Notifybuilder.setlargeicon (bitmap);
Notifybuilder.setsmallicon (SmallIcon);
Notifybuilder.setticker (Tickertext);
Notifybuilder.setcontentinfo (ContentInfo);
Notifybuilder.setcontenttitle (Contenttitle);
Notifybuilder.setcontenttext (ContentText);
Notifybuilder.setautocancel (FALSE);
Notifybuilder.setwhen (System.currenttimemillis ());
Notifybuilder.setdefaults (Notification.default_all);
Notifybuilder.setcontentintent (contentintent);
Notification Noti = Notifybuilder.build ();
Getnotificationmanager (Context). Notify (Notification_id_1, Noti);
}


public void showcustomviewnotify (context context, int smallicon,
Charsequence tickertext, int layoutid) {
Remoteviews mremoteviews = new Remoteviews (Context.getpackagename (),
LayoutID);
Pendingintent contentintent = pendingintent.getactivity (context, 0,
New Intent (context, Mainactivity.class), 0);
Notificationcompat.builder Notifybuilder = new Notificationcompat.builder (
context);
Notifybuilder.setsmallicon (SmallIcon);
Notifybuilder.setcontent (mremoteviews);
Notifybuilder.setticker (Tickertext);
Notifybuilder.setongoing (TRUE);
Notifybuilder.setautocancel (TRUE);
Notifybuilder.setdefaults (Notification.default_all);
Notifybuilder.setcontentintent (contentintent);

notification Noti = Notifybuilder.build ();
Intent dismissedintent = new Intent (
"com.xxxx.android.notify.dismissed");
/**
* The intent to execute when The notification is explicitly dismissed
* By the user, either with the ' Clear all ' "button or by swiping it away
* individually.

* This probably shouldn ' t is Launching an activity since several of
* Those would be sent at the same time.
*/
noti.deleteintent = Pendingintent.getbroadcast (context, 0,
dismissedintent, 0);

Getnotificationmanager (Context). Notify (Notification_id_2, Noti);
}


Public Notification getforegroundservicenotify (context context,
int SmallIcon, charsequence tickertext, Charsequence ContentInfo,
Charsequence Contenttitle, charsequence contenttext) {


Pendingintent contentintent = pendingintent.getactivity (context, 0,
New Intent (context, Mainactivity.class), 0);
Notificationcompat.builder Notifybuilder = new Notificationcompat.builder (
context);
Notifybuilder.setsmallicon (SmallIcon);
Notifybuilder.setticker (Tickertext);
Notifybuilder.setcontentinfo (ContentInfo);
Notifybuilder.setcontenttitle (Contenttitle);
Notifybuilder.setcontenttext (ContentText);
Notifybuilder.setautocancel (FALSE);
Notifybuilder.setcontentintent (contentintent);
Notifybuilder.setwhen (System.currenttimemillis ());
Notifybuilder.setdefaults (Notification.default_all);
return Notifybuilder.build ();
}
}


Android Notification Monitor system clears the notification bar method

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.