Android设置Notification从网络中加载图片,解决点击无法消失的bug
来源:互联网 发布:mac电脑双系统 编辑:程序博客网 时间:2024/06/08 22:19
Notification的构造函数中只支持使用资源文件的图片作为图片,但是如果我们想用网络中的图片怎么办呢。
我们知道,给Notification设置内容的时候调用的是setLatestEventInfo方法,当我们点击去看该方法的时候,所有的结果都一目了然了。
public void setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent 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, "setBackgroundResource", R.drawable.notification_template_icon_low_bg); contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource", R.drawable.notification_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; }
this.contentView = contentView; this.contentIntent = contentIntent;这两个方法来实现界面布局和Intent的,那么我们可不可以自己写一个remoteView呢,废话。。。
看RemoteViews中的
public void setImageViewBitmap(int viewId, Bitmap bitmap) { setBitmap(viewId, "setImageBitmap", bitmap); }这不就是我们想要的吗。。。。
我们只有一个目的,把图片换成网络中加载的图片,我们先把图片从网络中加载到本地,看代码:
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);// 设置超时conn.setDoInput(true);conn.setUseCaches(false);// 不缓存conn.connect();int code = conn.getResponseCode();Bitmap bitmap = null;if (code == 200) {InputStream is = conn.getInputStream();// 获得图片的数据流bitmap = BitmapFactory.decodeStream(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);}
我们从网络中把图片加载到本地形成了bitmap,然后通过setImageViewBitmap把bitmap设置进去就oK了。
再来看看openNitifNews方法:
/** * 处理推送的逻辑 * * @param ct * @param imgUrl * @param bundle */private void openNitifNews(Context ct, Bitmap bitmap, Bundle 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();// 采用默认声音notification.defaults |= Notification.DEFAULT_SOUND;// 使用默认的灯光notification.defaults |= Notification.DEFAULT_LIGHTS;// 通知被点击后,自动消失notification.flags |= Notification.FLAG_AUTO_CANCEL;notification.icon = R.drawable.ic_launcher;// 点击'Clear'时,无法清除通知// notification.flags |= Notification.FLAG_NO_CLEAR;// 点击清除按钮不会清除消息通知,可以用来表示在正在运行// notification.flags |= Notification.FLAG_ONGOING_EVENT;RemoteViews remoteView = new RemoteViews(context.getPackageName(),R.layout.notif_news); //通过自己的布局来显示remoteView.setImageViewBitmap(R.id.iv_newspush_icon, bitmap);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); //如果在这里设置点击事件,不会消失,根据需求来。。notification.contentView = remoteView; notification.contentIntent = contentIntent; nm.notify(MySmsReceiver.NEWSPUAH, notification);}
ok了,接着就是调用吧。。。
0 0
- Android设置Notification从网络中加载图片,解决点击无法消失的bug
- 解决android 在代码中设置view背景后,padding效果消失的bug
- android listview 中从网络加载图片
- 解决android中点击对话框外部,对话框消失的问题
- 解决android中点击对话框外部,对话框消失的问题
- Android notification动态设置网络图片icon
- Android 从网络加载图片
- 解决Android从相册中获取图片出错图片却无法裁剪问题的方法
- Android中网络图片的异步加载
- 解决Android从相册中获取图片却无法裁剪的问题
- 设置bootstrap模态窗页面点击无法消失
- webview无法正常加载和点击的bug
- (Android学习)ImageView设置异步加载的网络图片
- IE6版本中点击验证码旁边的“看不清”后验证码图片消失未加载出
- Android从网络上 异步加载图片
- android 从网络加载图片并显示
- libgdx中异步从网络加载图片
- 从网络异步加载listView中图片
- VS2008字体设置
- AngularJS学习笔记
- CharSequence的getText()与String的getString()
- row_number() OVER(PARTITION BY)函数介绍
- Find The Multiple
- Android设置Notification从网络中加载图片,解决点击无法消失的bug
- iOS开发UI篇—控制器的创建
- static_cast, dynamic_cast和reinterpret_cast的区别和应用
- OC学习总结(三)
- LeetCode: Largest Number
- SWIG and Python(用SWIG将C/C++封装为Python扩展模块)
- 排序算法之 快速排序 及其时间复杂度和空间复杂度
- nginx源码学习资源
- sysctl -P 报"unknown key"错误解决办法