"direct fetch with のandroid common code module parsing and sharing" のnotification and Notificationmanager

Source: Internet
Author: User
<span id="Label3"></p><p class="p1"><p class="p1"><span style="font-family:Comic Sans MS"><span style="font-size:18px"><span style="color:#3333ff">This article source code is hosted in https://github.com/ASCE1885/asce-common, Welcome fork</span></span></span></p></p><p class="p1"><p class="p1"><span class="s1"><span style="font-family:Comic Sans MS; font-size:18px">The Android project is doing much more. Will find that the original very many basic things are able to reuse, this series introduced some of their own projects are often used in the public module code (of course, Just talk about 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 efficiency.</span></span></p></p><p class="p1"><p class="p1"><span class="s1"><span style="font-family:Comic Sans MS; font-size:18px">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 just how to encapsulate into a public module. For use by the entire project.</span></span></p></p><p class="p1"><p class="p1"><span class="s1"><span style="font-family:Comic Sans MS; font-size:18px">Based on a different purpose. The appearance of the notification is very different, and the corresponding code is the difference in layout, 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 seen in the code listing Imynotificationbuilder.java.</span></span></p></p><p class="p1"><p class="p1"><span class="s1"></span></p></p><pre code_snippet_id="384476" snippet_file_name="blog_20140609_1_3801216" name="code" class="java"><pre code_snippet_id="384476" snippet_file_name="blog_20140609_1_3801216" name="code" class="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);}</pre></pre><br><p><p></p></p><p class="p1"><p class="p1"><span class="s1"><span style="font-family:Comic Sans MS; font-size:18px">See here, familiar with the design pattern of the students should know that we use the builder model to build different notification, according to the convention, generally there will be a default builder implementation, We named it Basicnotificationbuilder.java. It uses the default notification bar layout of the Android System.</span></span></p></p><p><p></p></p><pre code_snippet_id="384476" snippet_file_name="blog_20140609_2_3168361" name="code" class="java">/** * 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;}} </pre><br><p class="p1"><p class="p1"><span class="s1"><span style="font-family:Comic Sans MS; font-size:18px">For other forms of notification bars that require you to define your own Layout. We also implement a class. The layout file is defined by the user itself, as shown in the code listing Customnotificationbuilder.java.</span></span></p></p><pre code_snippet_id="384476" snippet_file_name="blog_20140609_3_5883577" name="code" class="java">/** * Define your own style notification constructor * * @author asce1885 * @date 2014-06-09 * */public class Customnotificationbuilder Implement s 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 = (Text View) 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;}}}</pre><br><p class="p1"><p class="p1"><span class="s1"><span style="font-family:Comic Sans MS; font-size:18px">Notification's Build code ends Here. And to emit notification, using notificationmanager, we encapsulate it as a code listing Mynotificationmanager.java.</span></span></p></p><pre code_snippet_id="384476" snippet_file_name="blog_20140609_4_8598792" name="code" class="html">/** * 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;}}</pre><br><p class="p1"><p class="p1"><span class="s1"><span style="font-family:Comic Sans MS; font-size:18px">finally, the Demo sample code to be used for example is shown below, just as a reference. Some of the variables inside are not to be delved into.</span></span></p></p><pre code_snippet_id="384476" snippet_file_name="blog_20140609_5_441956" name="code" class="html"><pre code_snippet_id="384476" snippet_file_name="blog_20140609_5_441956" name="code" class="html">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);</pre></pre><br><span style="line-height:26px; font-family:‘Comic Sans MS‘; font-size:18px"><span style="line-height:26px; font-family:‘Comic Sans MS‘; font-size:18px">--welcome Reprint. Please indicate the source</span></span><span style="font-family:‘Comic Sans MS‘; font-size:18px"><span style="font-family:‘Comic Sans MS‘; font-size:18px">http://blog.csdn.net/asce1885</span></span><span style="line-height:26px; font-family:‘Comic Sans MS‘; font-size:18px"><span style="line-height:26px; font-family:‘Comic Sans MS‘; font-size:18px">, do not use for commercial purposes without my Permission. Thank you--</span></span><br> <p><p>"direct fetch with のandroid common code module parsing and sharing" のnotification and Notificationmanager</p></p></span>

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.