Android project to do more, will find that many of the original things are reusable, this series introduced some of their own projects commonly used in the public module code (of course, only talk about the technology do not talk about business), one to sort out their own later can be directly used, and secondly also share to everyone, hoping to slightly reduce the overtime time for everyone, Improve a little bit of efficiency.
The principle and role of Android notification is not explained here, it is believed that an Android developer has used more than once, the following only describes how to encapsulate a public module for the entire project to use.
Depending on the purpose, the appearance of notification is very different, corresponding to the code is the layout of the difference, so we first need to have an interface for the user to create a different layout of the notification, the interface in Java is of course in the form of interface, As shown in the code listing Imynotificationbuilder.java.
/** * Notification Interface * * @author asce1885 * @date 2014-06-09 * */public interface Imynotificationbuilder {public Stati C final String notification_id = IMyNotificationBuilder.class.getSimpleName (); Notification buildnotification (string title, string content);}
See here, students familiar with the design pattern should know that we are using the builder model to build different notification, according to Convention, there will usually be a default builder implementation, We named it Basicnotificationbuilder.java, which uses the default notification bar layout of the Android system.
/** * Default Notification constructor * * @author asce1885 * @date 2014-06-09 * */public class Basicnotificationbuilder implements IMy Notificationbuilder {private Context mcontext;private pendingintent mpendingintent;public int micondrawableid;public Basicnotificationbuilder (context context, pendingintent pendingintent) {mcontext = Context;mpendingintent = Pendingintent;micondrawableid = Packageutils.getappinfo (context). Icon;} Public Basicnotificationbuilder (context context, pendingintent pendingintent, int icondrawableid) {mcontext = context; Mpendingintent = Pendingintent;micondrawableid = Icondrawableid;} @SuppressWarnings ("deprecation") @Overridepublic Notification buildnotification (string title, string content) { Notification basicnotification = new Notification (Micondrawableid, Title,system.currenttimemillis ()); Basicnotification.setlatesteventinfo (Mcontext, title, content, mpendingintent); Basicnotification.flags |= Notification.flag_auto_cancel;basicnotification.defaults = Notification.default_soUnd;basicnotification.contentintent = Mpendingintent;return basicnotification;}}
For other forms of notification bars that require a custom layout, we also implement a class where the layout file is customized by the user, as shown in the code listing Customnotificationbuilder.java.
/** * Custom Style notification constructor * * @author asce1885 * @date 2014-06-09 * */public class Customnotificationbuilder implements Imynotificationbuilder {private Context mcontext;private int mcustomelayout;private int mlayoutsubjectid;private int mlayoutmessageid;private int mlayouticonid;private int mstatusbaricondrawableid;private Uri mSoundUri;private Pendingintent Mpendingintent;public Customnotificationbuilder (context context, int customelayout, int layoutsubjectid , int layoutmessageid, int layouticonid, int statusbaricondrawableid, Uri Sounduri, pendingintent pendingintent) {MContex t = context;mcustomelayout = Customelayout;mlayoutsubjectid = Layoutsubjectid;mlayoutmessageid = LayoutMessageId; Mlayouticonid = Layouticonid;mstatusbaricondrawableid = Statusbaricondrawableid;msounduri = SoundUri;mPendingIntent = Pendingintent;} @SuppressWarnings ("deprecation") @Overridepublic Notification buildnotification (string title, string content) {if ( Textutils.isempty (content)) {return null;} PrefroyonoTificationstylediscover.getinstance (). Discoverstyle (Mcontext); Notification customernotification = new Notification (Mstatusbaricondrawableid, Title,system.currenttimemillis ()); Remoteviews Customerremoteview = new Remoteviews (Mcontext.getpackagename (), mcustomelayout); Customerremoteview.settextviewtext (Mlayoutsubjectid, Packageutils.getappname (Mcontext)); Customerremoteview.settextviewtext (Mlayoutmessageid, content); Customerremoteview.setimageviewresource ( Mlayouticonid, Mstatusbaricondrawableid); Customernotification.flags |= notification.flag_auto_cancel;if (MSoundUri ! = null) {customernotification.sound = Msounduri;} else {customernotification.defaults = Notification.default_sound;} Customernotification.contentintent = mpendingintent;//Some SAMSUNG devices status bar cant ' t show the lines with the size ,//So need to verify it, maybe increase the height or decrease the font sizecustomerremoteview.setfloat (Mlayoutsubjectid, "Settextsize", Prefroyonotificationstylediscover.getinstance (). GettItlesize ()); Customerremoteview.settextcolor (Mlayoutsubjectid, Prefroyonotificationstylediscover.getinstance (). Gettitlecolor ()); Customerremoteview.setfloat (Mlayoutmessageid, "Settextsize", Prefroyonotificationstylediscover.getinstance (). GetTextSize ()); Customerremoteview.settextcolor ( Mlayoutmessageid, Prefroyonotificationstylediscover.getinstance (). GetTextColor ()); Customernotification.contentview = Customerremoteview;return customernotification;} /** * A class for discovering Android Notification styles on Pre-froyo (2.3) devices */private static class Prefroyonotifi Cationstylediscover {private Integer mnotifytextcolor = null;private float mnotifytextsize = 11;private integer Mnotifyti Tlecolor = null;private Float mnotifytitlesize = 12;private final String text_search_text = "Searchfortext";p rivate final String text_search_title = "Searchfortitle";p rivate static Context mcontext;private static class Lazyholder {private stat IC final Prefroyonotificationstylediscover sinstance = new PrefroyoNotificationstylediscover ();} public static Prefroyonotificationstylediscover getinstance () {return lazyholder.sinstance;} Private Prefroyonotificationstylediscover () {}public int gettextcolor () {return mnotifytextcolor.intvalue ();} public float GetTextSize () {return mnotifytextsize;} public int Gettitlecolor () {return mnotifytitlecolor;} public float gettitlesize () {return mnotifytitlesize;} private void Discoverstyle (context context) {Mcontext = context;//already doneif (null! = Mnotifytextcolor) {return;} try {Notification notify = new Notification (); Notify.setlatesteventinfo (Mcontext, Text_search_title, Text_search_text , null); LinearLayout Group = new LinearLayout (mcontext); ViewGroup event = (viewgroup) notify.contentView.apply (Mcontext, group); Recursegroup (event); Group.removeallviews ();} catch (Exception e) {//Default to Somethingmnotifytextcolor = Android. R.color.black;mnotifytitlecolor = Android. R.color.black;}} Private Boolean Recursegroup (ViewGroup Group) {Final int count = group.geTchildcount (); for (int i = 0; i < count; ++i) {if (Group.getchildat (i) instanceof TextView) {final TextView TV = (TEXTV Iew) Group.getchildat (i); final String text = Tv.gettext (). toString (); if (Text.startswith ("Searchfor")) { Displaymetrics metrics = new Displaymetrics (); WindowManager wm = (WindowManager) mcontext.getsystemservice (Context.window_service); Wm.getdefaultdisplay (). Getmetrics (metrics); if (Text_search_text = = TEXT) {Mnotifytextcolor = Tv.gettextcolors (). Getdefaultcolor (); Mnotifytextsize = Tv.gettextsize (); mnotifytextsize/= metrics.scaleddensity;} else {Mnotifytitlecolor = Tv.gettextcolors (). Getdefaultcolor (); mnotifytitlesize = Tv.gettextsize (); mNotifyTitleSize /= metrics.scaleddensity;} if (null! = Mnotifytitlecolor && Mnotifytextcolor! = null) {return true;}}} else if (Group.getchildat (i) instanceof ViewGroup) {if (Recursegroup (viewgroup) Group.getchildat (i)) {return true;}}} return false;}}}
Notification's build code ends here, and to emit notification, using Notificationmanager, we encapsulate it as a code listing Mynotificationmanager.java.
/** * Notification Manager * * @author asce1885 * @date 2014-06-09 * */public class Mynotificationmanager {private imynotific Ationbuilder mmynotificationbuilder;private static final int start_id = 1000;private static final int RANGE = 50;private I NT Mcurrentid = start_id;private Mynotificationmanager () {}private static class Lazyholder {private static final mynotific Ationmanager sinstance = new Mynotificationmanager ();} public static Mynotificationmanager getinstance () {return lazyholder.sinstance;} Public Imynotificationbuilder Getmynotificationbuilder () {return mmynotificationbuilder;} public void Setmynotificationbuilder (Imynotificationbuilder builder) {mmynotificationbuilder = builder;} public void DeliverNotification (context context, string title, string content) {Buildanddisplaynotification (context, Title, content);} private void Buildanddisplaynotification (context context, string title, string content) {if (null! = Mmynotificationbuild ER) {Notification Notification = MmynotificationbuilDer.buildnotification (title, content); if (null! = notification) {notification.flags |= notification.flag_auto_cancel; Notificationmanager Notificationmanager = (notificationmanager) context.getsystemservice (Context.NOTIFICATION_ SERVICE); Notificationmanager.notify (Generatenotification (), notification);}}} private int generatenotification () {mcurrentid++;if (Mcurrentid >= start_id + RANGE) {Mcurrentid = start_id;} return Mcurrentid;}}
The last example code to use is shown below, and as a reference, some of the variables are not to be delved into.
Intent Intent = new Intent (Getapplicationcontext (), mainactivity.class); Intent.putextra (Bundleconstant.key_ Notification_type, action); Intent.putextra (Bundleconstant.key_webview_title, notice.webview_title); Intent.putextra (Bundleconstant.key_webview_url, Notice.webview_url); Pendingintent pendingintent = pendingintent.getactivity (Getapplicationcontext (), (int) Notice.nid, intent, pendingintent.flag_update_current); builder = new Basicnotificationbuilder (Getapplicationcontext (), PendingIntent); Mynotificationmanager.getinstance (). Setmynotificationbuilder (builder); Mynotificationmanager.getinstance (). DeliverNotification (Getapplicationcontext (), Notice.notification_title, Notice.notification_content);
--Welcome reprint, please indicate the sourcehttp://blog.csdn.net/asce1885, do not use for commercial purposes without my consent, thank you--