android 如何自定义通知栏图标(不使用自定义布局情况下)
2016-05-05 18:09
696 查看
自定义通知栏图标?不是很简单么。自定义布局都不在话下!
是的,有xml布局文件当然一切都很简单,如果不给你布局文件用呢?
听我慢慢道来!
首先怎么创建一个通知呢?
1.new 一个
[java] view
plain copy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
Notification n = new Notification(android.R.drawable.ic_menu_share, null, System.currentTimeMillis());
参数:图标 ID,发送到状态栏瞬间的文字,当前时间
2.设置详细信息:标题、内容、intent
[java] view
plain copy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
PendingIntent contentIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
n.setLatestEventInfo(this, "早上好!", "今天是个晴朗的天气!", contentIntent);
3.发送到通知栏
[java] view
plain copy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
NotificationManager mNM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNM.notify(1001, n);
这样就完成了一个通知的展示,很简单!
我们来看看 n.setLatestEventInfo 干了些什么呢
[java] view
plain copy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
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;
}
可以看到,他实际上就是使用系统默认布局为我们创建了一个 RemoteViews ,RemoteViews 是专门用来跨进程显示的 View ,详情参考官方文档:http://developer.android.com/intl/zh-cn/reference/android/widget/RemoteViews.html
看这句:
[java] view
plain copy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
contentView.setImageViewResource(R.id.icon, this.icon);
其实就是设置图标了:
参数1:用来显示图标的 ImageView 的 id
参数2:图标 id
但是还有一个这样的方法:
[java] view
plain copy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
RemoteViews.setImageViewBitmap(int viewId, Bitmap bitmap)
用 Bitmap 来设置图标。
而 Notifycation 里面有个参数:Notification.contentView,仔细看,setLastestEventInfo 方法里创建的 RemoteViews 就是他,所以你知道该怎么做了!
但是这里还有一个问题?R.id.icon 怎么获取,这个东西其实在 com.Android.internal.R
这个里面,但是这个类我们访问不到怎么办?
反射呗, Java 的反射可谓是万能啊,啥都可以拿到只要他在。
[java] view
plain copy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
Class<?> clazz = Class.forName("com.android.internal.R$id");
Field field = clazz.getField("icon");
field.setAccessible(true);
int id_icon = field.getInt(null);
[java] view
plain copy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
n.setLatestEventInfo(context, title, msg, contentIntent);
n.flags |= Notification.FLAG_AUTO_CANCEL;
if(n.contentView != null && icon != null){
n.contentView.setImageViewBitmap(id_icon, icon);
}
发出通知,下拉通知栏看看,图标是不是变了^_^
此外这里还有一个小细节,就是你 new Notifycation() 是传进去的图标会作为状态栏的小图标,小图标尺寸在 hdpi 下面放 32x32 的就可以
![](https://oscdn.geek-share.com/Uploads/Images/Content/201605/d7a5f5b45f9185e991fc4980eabc93b4)
所以你可以第一次传小图标,然后通过 contentView 设置大图标,这样就OK了
![](https://oscdn.geek-share.com/Uploads/Images/Content/201605/b970ee78db96c60f7c4e49d88684be97)
是的,有xml布局文件当然一切都很简单,如果不给你布局文件用呢?
听我慢慢道来!
首先怎么创建一个通知呢?
1.new 一个
[java] view
plain copy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
Notification n = new Notification(android.R.drawable.ic_menu_share, null, System.currentTimeMillis());
参数:图标 ID,发送到状态栏瞬间的文字,当前时间
2.设置详细信息:标题、内容、intent
[java] view
plain copy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
PendingIntent contentIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
n.setLatestEventInfo(this, "早上好!", "今天是个晴朗的天气!", contentIntent);
3.发送到通知栏
[java] view
plain copy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
NotificationManager mNM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNM.notify(1001, n);
这样就完成了一个通知的展示,很简单!
我们来看看 n.setLatestEventInfo 干了些什么呢
[java] view
plain copy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
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;
}
可以看到,他实际上就是使用系统默认布局为我们创建了一个 RemoteViews ,RemoteViews 是专门用来跨进程显示的 View ,详情参考官方文档:http://developer.android.com/intl/zh-cn/reference/android/widget/RemoteViews.html
看这句:
[java] view
plain copy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
contentView.setImageViewResource(R.id.icon, this.icon);
其实就是设置图标了:
参数1:用来显示图标的 ImageView 的 id
参数2:图标 id
但是还有一个这样的方法:
[java] view
plain copy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
RemoteViews.setImageViewBitmap(int viewId, Bitmap bitmap)
用 Bitmap 来设置图标。
而 Notifycation 里面有个参数:Notification.contentView,仔细看,setLastestEventInfo 方法里创建的 RemoteViews 就是他,所以你知道该怎么做了!
但是这里还有一个问题?R.id.icon 怎么获取,这个东西其实在 com.Android.internal.R
这个里面,但是这个类我们访问不到怎么办?
反射呗, Java 的反射可谓是万能啊,啥都可以拿到只要他在。
[java] view
plain copy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
Class<?> clazz = Class.forName("com.android.internal.R$id");
Field field = clazz.getField("icon");
field.setAccessible(true);
int id_icon = field.getInt(null);
[java] view
plain copy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
n.setLatestEventInfo(context, title, msg, contentIntent);
n.flags |= Notification.FLAG_AUTO_CANCEL;
if(n.contentView != null && icon != null){
n.contentView.setImageViewBitmap(id_icon, icon);
}
发出通知,下拉通知栏看看,图标是不是变了^_^
此外这里还有一个小细节,就是你 new Notifycation() 是传进去的图标会作为状态栏的小图标,小图标尺寸在 hdpi 下面放 32x32 的就可以
所以你可以第一次传小图标,然后通过 contentView 设置大图标,这样就OK了
相关文章推荐
- Android学习之界面篇(八)在项目中使用Action Bar
- android中怎样把一个button按钮放到屏幕底部
- 5分钟搞定android混淆
- android studio中安装parcelable插件
- Android Studio导入Vitamio问题详细解析
- android 缓存思路
- 学习Android动画小结
- Android 编写文件浏览器
- Android Handler
- 怎样将Activity设置成窗口模式
- android开发通知常用设置方法
- Parcelable的使用,以及与Serializable的区别(最后有实例)
- Android 系统Action大全
- Freescale imx6的android lvds屏幕驱动调试
- Android 拍照、选择图片并裁剪
- Android Toast无延时处理
- android在线API文档
- iOS,Android,Jave后台AES加密解密
- android:TextView中的文本链接之--链接的几种方式
- 【转】android获取屏幕宽度和高度