http://mrfu.me/2016/02/27/Glide_Loading_Images_into_Notifications_and_AppWidgets/ Load pictures to notifications
The notification bar icon is an important context for the user. Use Notificationcompat.builder to set up a large notification picture directly, but the image must be in Bitmap form. If the picture is already available on the phone, that's fine. However, if the picture bin is not on the device and needs to be loaded from the web, it becomes impossible to deal with it in a standard way.
Let Glide do it. In the previous blog, we saw how to use simpletarget to download pictures in Bitmap form. In theory, you can use this method to load pictures into your notification bar. But this is not necessary, because Glide provides a more convenient and comfortable way:notificationtarget. Notificationtarget
So let's look at the code. Now you know how Glide target works, so we're not going to use it anymore. To display a large picture in the notification bar, you can use remoteviews and display a custom notification bar.
Our custom notification bar is simpler:
Custom Layout Remoteview_notification.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/an" Droid "Android:layout_width=" Match_parent "android:layout_height=" wrap_content "android:background=" @android: C
Olor/white "android:orientation=" vertical "> <linearlayout android:layout_width=" match_parent "
android:layout_height= "wrap_content" android:orientation= "horizontal" android:padding= "2DP" >
<imageview android:id= "@+id/remoteview_notification_icon" android:layout_width= "50DP"
android:layout_height= "50DP" android:layout_marginright= "2DP" android:layout_weight= "0" Android:scaletype= "Centercrop"/> <linearlayout android:layout_width= "0DP" Andro
id:layout_height= "Wrap_content" android:layout_weight= "1" android:orientation= "vertical" >
<textview Android:id= "@+id/remoteview_notification_headline" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:ellipsize= "End" android:singleline= " True "android:textsize=" 12sp "/> <textview android:id=" @+id/remoteview_no Tification_short_message "android:layout_width=" match_parent "android:layout_height=" Wrap_ Content "Android:ellipsize=" "End" android:paddingbottom= "2DP" Android:singl Eline= "true" android:textsize= "14sp" android:textstyle= "bold"/> </linearlayo Ut> </LinearLayout> </LinearLayout>
The following code creates a custom notification for us using the above layout file.
Final Remoteviews RV = new Remoteviews (Context.getpackagename (), r.layout.remoteview_notification);
Rv.setimageviewresource (R.id.remoteview_notification_icon, R.mipmap.future_studio_launcher);
Rv.settextviewtext (R.id.remoteview_notification_headline, "headline");
Rv.settextviewtext (r.id.remoteview_notification_short_message, "short message"); Build notification Notificationcompat.builder Mbuilder = new Notificationcompat.builder (context). setsmal
Licon (R.mipmap.future_studio_launcher). Setcontenttitle ("Content Title"). Setcontenttext ("Content Text")
. SetContent (RV). setpriority (Notificationcompat.priority_min);
Final Notification Notification = Mbuilder.build (); Set big content view for newer androids if (Android.os.Build.VERSION.SDK_INT >=) {Notification.bigcontentv
Iew = RV; } Notificationmanager Mnotificationmanager = (notificationmanager) context.getsystemservice (Context.NOTIFICATION_
SERVICE); MnotificationmaNager.notify (notification_id, NOTIFICATION);
This snippet creates three important objects for us, notification and remoteviews, and constant notification_id. We'll need these to create a notification target.
Private Notificationtarget Notificationtarget;
...
Notificationtarget = new Notificationtarget (context
,
RV,
R.id.remoteview_notification_icon,
notification,
notification_id);
And finally, we're going to call Glide, as we did on the blog before, and Target as . into () parameters.
Glide
. With (Context.getapplicationcontext ())//safer!
. Load (eatfoodyimages[3])
. Asbitmap (). into
(notificationtarget);
App Widgets
Let's look at another Glide target. The application widget has been part of Android for a long time. If your APP provides widgets and includes images, this part should be interesting to you. Glide's Appwidgettarget can significantly make you very simple to achieve.
Let's look at a simple Appwidgetprovider Example:
public class Fsappwidgetprovider extends Appwidgetprovider {private Appwidgettarget appwidgettarget; @Override public void OnUpdate (context context, Appwidgetmanager Appwidgetmanager, int[] Appw
Idgetids) {remoteviews RV = new Remoteviews (Context.getpackagename (), R.layout.custom_view_futurestudio);
Appwidgettarget = new Appwidgettarget (context, RV, r.id.custom_view_image, appwidgetids);
Glide. With (Context.getapplicationcontext ())//safer!
. Load (Glideexampleactivity.eatfoodyimages[3]). Asbitmap (). into (Appwidgettarget);
Pushwidgetupdate (context, RV); public static void Pushwidgetupdate (context context, Remoteviews RV) {componentname mywidget = new Compone
Ntname (context, fsappwidgetprovider.class);
Appwidgetmanager Manager = appwidgetmanager.getinstance (context);
Manager.updateappwidget (Mywidget, RV); }
}
A few lines of important code declares the Appwidgettarget object and the builder of the Glide. The advantage here is that you don't need to customize Appwidgettarget and override any Appwidgettarget method. Glide has done it automatically for you. That's great.