android自定义Notification通知栏实例
项目有个需求,需要在发送Notification的时候动态给定url的图片。大概思路如下:自己定义一个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>
android代码:
- 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 rv = new RemoteViews(this.getPackageName(),
- R.layout.cus_noti);
- rv.setImageViewResource(R.id.image,
- R.drawable.ic_launcher);
- rv.setImageViewBitmap(R.id.image, bitmap);
- rv.setTextViewText(R.id.text,
- "Hello,this message is in a custom expanded view");
- noti.contentView = rv;
- // 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);
- }
- }
android自定义Notification通知栏实例的更多相关文章
- Android自定义Notification并没有那么简单
背景 最近需要实现一个自定义Notification的功能.网上找了找代码,解决方案就是通过RemoteViews来实现.但是在实现过程中遇到不少问题,网上也没有很好的文章描述这些问题,所以在这里做个 ...
- android 自定义通知栏
package com.example.mvp; import cn.ljuns.temperature.view.TemperatureView;import presenter.ILoginPre ...
- Android -- 系统和自定义Notification
Notification是一种让你的应用程序在不使用Activity的情况下警示用户,Notification是看不见的程序组件警示用户有需要注意的事件发生的最好途径. 作为UI部分,Notifica ...
- Android学习(二十)Notification通知栏
一.通知栏的内容 1.图标 2.标题 3.内容 4.时间 5.点击后的相应 二.如何实现通知栏 1.获取NotificationManager. 2.显示通知栏:notify(id,notificat ...
- Android Notification通知栏使用
package com.example.mynotifycation; import android.app.Activity; import android.app.Notification; im ...
- Android开发——Notification通知的使用及NotificationCopat.Builder常用设置API
想要看全部设置的请看这一篇 [转]NotificationCopat.Builder全部设置 常用设置: 设置属性 说明 setAutoCancel(boolean autocancel) 设置点击信 ...
- android的消息通知栏
在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...
- Android开发——Notification通知的各种Style详解
本来是想与之前讲解使用Notification通知使用一起写的,查看了资料,觉得有必要将这Style部分单独拿出来讲解 前篇:Android开发——Notification通知的使用及Notifica ...
- android拾遗——Android之Notification和NotificationManager
1.使用系统自带的Notification //创建一个NotificationManager的引用 String ns = Context.NOTIFICATION_SERVICE; Notific ...
随机推荐
- git常用命令,学git代码管理
下面是我整理的常用 Git 命令清单.几个专用名词的译名如下. Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remote:远程仓库 一. ...
- 3.移植驱动到3.4内核-移植DM9000C驱动
在上章-使内核支持烧写yaffs2,裁剪内核并制作补丁了 本章,便开始移植以前2.6内核的驱动到3.4新内核 1.介绍 首先内核更新,有可能会重新定义新的宏,去除以前的宏,以前更改函数名等 所以移植驱 ...
- Jmeter之http性能测试实战 非GUI模式压测 NON-GUI模式 结果解析TPS——干货(十一)
性能测试计划 性能测试用例 录制脚本 性能测试结果 性能测试报告 性能测试监控报告 准备工作 从脚本已录制成功之后开始进行压测 安装Jmeter拓展插件 查看 Transactions per Sec ...
- SQL性能优化的几点建议
1. 索引:索引可以提高查询的速度,但不是使用带有索引的字段查询时,索引都会起作用,如下几种特殊情况下,有可能使用带有索引的字段查询时,索引没有起作用:1)使用LIKE关键字的查询语句 如果匹配字符串 ...
- js中如何处理大量有规律的变量
var a1=document.getElementById('a1'); var a1=document.getElementById('a2'); var a1=document.getEleme ...
- 全球(局)唯一标识符GUID的使用
1.GUID百科介绍: 1.全局唯一标识符(GUID,Globally Unique Identifier)也称作 UUID(Universally Unique IDentifier) .GUID是 ...
- 配置可以通过http协议访问的svn服务器
通过HTTP协议访问版本库是Subversion的亮点之一,这种方式具备许多svnserve服务器所没有的特性,使用上更加灵活. 关于mode_day_svn模块: 由于Subversion需要版本化 ...
- Linux终端下 dstat 监控工具
dstat 是一个可以取代vmstat,iostat,netstat和ifstat这些命令的多功能产品.dstat克服了这些命令的局限并增加了一些另外的功能,增加了监控项,也变得更灵活了.dstat可 ...
- [Ccodeforces 736C] Ostap and Tree - 树形DP
给定一个n个点的树,把其中一些点涂成黑色,使得对于每个点,其最近的黑点的距离不超过K. 树形DP. 设置状态f[i][j]: 当j <= K时: 合法状态,表示i的子树中到根的最近黑点距离为j的 ...
- [HNOI2009] 梦幻布丁
[HNOI2009] 梦幻布丁 标签: 链表 题解 可以直接用链表启发式合并做. 合并的细节处理稍微有点麻烦. 假如需要变成另一种颜色的那个颜色的个数更多,那么就肯定不能直接合. 维护一个color数 ...
