Android notification动态设置网络图片icon
来源:互联网 发布:考研中南财经大学知乎 编辑:程序博客网 时间:2024/06/11 14:01
公司项目有个需求,需要在发送Notification的时候动态加载服务器的图片,在网上找了半天,没有完整的代码。研究了半天总算实现了。大概思路如下
自定义Notification的布局文件,这样能够很方便设置View的属性。
首先加载网络图片,使用BitmapFactory.decodeStream解析出Bitmap,然后设置到自定义布局文件中的ImageView上。
自定义Notification布局如下:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <ImageView
- android:id="@+id/image"
- android:layout_width="45dip"
- android:layout_height="45dip"
- android:layout_alignParentLeft="true"
- android:layout_marginBottom="8.0dip"
- android:layout_marginLeft="8.0dip"
- android:layout_marginRight="10dp"
- android:layout_marginTop="8.0dip" />
- <TextView
- android:id="@+id/title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="8.0dip"
- android:layout_toRightOf="@id/image"
- android:textSize="16.0dip" />
- <TextView
- android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/title"
- android:layout_marginTop="3.0dip"
- android:layout_toRightOf="@id/image"
- android:textSize="16.0dip" />
- <TextView
- android:id="@+id/time"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_centerVertical="true"
- android:layout_marginRight="8.0dip"
- android:textSize="16.0dip" />
- </RelativeLayout>
Java实现代码:
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import android.app.Activity;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.RemoteViews;
- public class MainActivity extends Activity {
- private String url = "http://www.takungpao.com/world/content/image/attachement/jpg/site2/20120605/d4bed9b92d221137df0511.jpg";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- set(url);
- }
- });
- }
- public void set(String urlStr) {
- new AsyncTask<String, Void, Bitmap>() {
- @Override
- protected 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;
- }
- }
- @Override
- protected void onPostExecute(Bitmap result) {
- super.onPostExecute(result);
- if (result != null) {
- showNotification(result);
- }
- }
- }.execute(urlStr);
- }
- private void showNotification(Bitmap bitmap){
- NotificationManager manager = (NotificationManager) MainActivity.this
- .getSystemService(Context.NOTIFICATION_SERVICE);
- Notification noti = new Notification();
- noti.flags = Notification.FLAG_AUTO_CANCEL;
- noti.icon = R.drawable.ic_launcher;
- // 1、创建一个自定义的消息布局 notification.xml
- // 2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段
- RemoteViews remoteView = new RemoteViews(this.getPackageName(),
- R.layout.cus_noti);
- remoteView.setImageViewResource(R.id.image,
- R.drawable.ic_launcher);
- remoteView.setImageViewBitmap(R.id.image, bitmap);
- remoteView.setTextViewText(R.id.text,
- "Hello,this message is in a custom expanded view");
- noti.contentView = remoteView;
- // 3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法)
- // 这儿点击后简答启动Settings模块
- PendingIntent contentIntent = PendingIntent.getActivity
- (MainActivity.this, 0,new
- Intent("android.settings.SETTINGS"), 0);
- noti.contentIntent = contentIntent;
- manager.notify(1, noti);
- }
- }
0 0
- Android notification动态设置网络图片icon
- Android notification icon 变成白色
- Android设置Notification从网络中加载图片,解决点击无法消失的bug
- 动态设置cusor & icon
- Android 5.0 notification icon is blank square
- 动态设置form的icon
- Flex button icon动态设置
- android 动态设置ImageView图片
- android Notification动态更新
- Flex动态设置Button中的icon指向外网图片源
- android应用icon动态更换
- Android ActionBar-左上角icon设置
- Android Listview异步动态加载网络图片
- android viewpager 动态从网络获取图片
- Android SMS intercept without notification icon or WAP-PUSH messages
- Android 消息推送(notification) 标题栏icon不显示内容
- Notification bar icon turns white in Android 5 Lollipop
- Android之TextView动态设置图片
- 用Restlet创建面向资源的服务
- c++中关于const的一些细节问题
- php设计模式——UML类图
- HTTP cookies 详解
- Android USB Host
- Android notification动态设置网络图片icon
- 判别给定表达式中所含括号是否正确配对出现的算法
- 股票交易模型简介
- hdu 4499 Cannon(暴力)
- POJ 1945 Power Hungry Cows 我的解法
- C++ 函数返回二维数组
- Vitamio中文API文档(2)—— VideoView
- 大师法发大水
- ICE笔记(08):C++线程与并发