Android settings notification load images from the network to resolve bugs that cannot be clicked

Source: Internet
Author: User

The notification constructor only supports images with resource files, but what if we want to use images from the Web?

We know that the Setlatesteventinfo method is called when the content is set to notification, and when we click to see the method, all the results are at a glance.

public void Setlatesteventinfo (context context, charsequence Contenttitle, Charsequence ContentText, Pendingint Ent contentintent) {//Todo:rewrite this to use Builder remoteviews Contentview = new Remoteviews (context.        Getpackagename (), r.layout.notification_template_base);        if (This.icon! = 0) {Contentview.setimageviewresource (R.id.icon, This.icon); } if (priority < Priority_low) {Contentview.setint (R.id.icon, "Setbackgroundresou            Rce ", R.DRAWABLE.NOTIFICATION_TEMPLATE_ICON_LOW_BG); Contentview.setint (r.id.status_bar_latest_event_content, "Setbackgroundresource", R.drawable.notificati        On_bg_low);        } if (Contenttitle! = null) {Contentview.settextviewtext (r.id.title, contenttitle);        } if (ContentText! = null) {Contentview.settextviewtext (R.id.text, ContentText);        } if (This.when! = 0) {    Contentview.setviewvisibility (R.id.time, view.visible);        Contentview.setlong (R.id.time, "settime", when);            } if (This.number! = 0) {NumberFormat f = numberformat.getintegerinstance ();        Contentview.settextviewtext (R.id.info, F.format (This.number));        } This.contentview = Contentview;    This.contentintent = contentintent; }


from the above code we can clearly see that notification used a remoteviews class to implement its own layout, and finally by invoking the

  This.contentview = Contentview;  This.contentintent = contentintent;
these two methods to achieve the interface layout and intent, then we can write a remoteview it, nonsense ...

Look at the remoteviews.

public void Setimageviewbitmap (int viewId, Bitmap Bitmap) {        setbitmap (viewId, "Setimagebitmap", Bitmap);    }
isn't that what we want?

We have only one purpose, to replace the image with the image loaded on the network, we first load the image from the network to local, see the code:

public void Set (final context context, String Urlstr, Final bundle bundle) {New asynctask<string, void, bitmap> () {@ overrideprotected Bitmap doinbackground (String ... params) {try {URL url = new URL (params[0]); HttpURLConnection conn = (httpurlconnection) url.openconnection (); conn.setconnecttimeout (6000);// Set timeout Conn.setdoinput (true), conn.setusecaches (false);//Do not cache conn.connect (); int code = Conn.getresponsecode (); Bitmap Bitmap = null;if (Code = =) {InputStream is = Conn.getinputstream ();//data stream for picture Bitmap = Bitmapfactory.decodestre AM (IS);} return bitmap;} catch (Malformedurlexception e) {e.printstacktrace (); return null;} catch (IOException e) {e.printstacktrace (); return null;}} @Overrideprotected void OnPostExecute (Bitmap result) {Super.onpostexecute (result); if (result! = null) {Opennitifnews ( Context, result, bundle);}}. Execute (URLSTR);}

we loaded the image from the network into the local formation of the bitmap, and then through the Setimageviewbitmap to set the bitmap in the OK.

Look again at the Opennitifnews method:

/** * Handling Push logic * * @param CT * @param imgurl * @param bundle */private void opennitifnews (Context ct, Bitmap Bitmap, Bundl E bundle) {Context context = Ct.getapplicationcontext (); Newsbean Bean = Null;bean = (Newsbean) bundle.getserializable ("Bean"), if (bean = = null) return;if (Textutils.isempty ( Bean.gettitle ())) {return;} if (Textutils.isempty (Bean.getdes ())) {Bean.setdes ("");} Notificationmanager nm = null;if (null = = Nm) {nm = (Notificationmanager) context.getsystemservice (context.notification_ SERVICE);} Notification Notification = new Notification ()//with default sound notification.defaults |= notification.default_sound;// Use the default light notification.defaults |= notification.default_lights;//notification is clicked and automatically disappears Notification.flags |= notification.flag_ Auto_cancel;notification.icon = r.drawable.ic_launcher;//When you click ' Clear ', the notification cannot be cleared//Notification.flags |= notification.flag_no_clear;//Click the Clear button will not clear the message notification, can be used to indicate in the running//Notification.flags |= notification.flag_ongoing_event; Remoteviews RemoteView = new Remoteviews (context.getpaCkagename (), r.layout.notif_news); Display Remoteview.setimageviewbitmap (R.id.iv_newspush_icon, bitmap) through its own layout; Remoteview.settextviewtext (R.id.tv_ Newpush_title, Bean.gettitle ()); Remoteview.settextviewtext (R.id.tv_newpush_des, Bean.getdes ()); Intent appIntent = New Intent (CT, Newsdetailsactivity.class), Appintent.putextra ("Article_type", Bean.gettypeid ()); Appintent.putextra ("news_id", Bean.getid ()); Appintent.putextra ("Headtitle", Bean.getheadtitle ()); Pendingintent contentintent = pendingintent.getactivity (CT, 0, appintent,pendingintent.flag_update_current);< Span style= "White-space:pre" ></span>
<span style= "White-space:pre" ></span>//remoteview.setonclickpendingintent (R.id.ll_newpush_root,< Span style= "White-space:pre" ></span>//contentintent);  If the Click event is set here, it will not disappear, depending on the requirements. Notification.contentview = RemoteView; Notification.contentintent = contentintent; Nm.notify (MYSMSRECEIVER.NEWSPUAH, notification);}

OK, then call it ...


Android settings notification load images from the network to resolve bugs that cannot be clicked

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.