毕业工作已经快半年了,打算在2016年开始总结自己平时在工作终于到的android方面的问题。今年遇到的第一个问题就是关于android的通知栏的问题。
在创建Notification的时候,总共有三种不同的方式。
(1)普遍的创建方式(通用)setLatestEventInfo(Context,”“,content,pendingIntent);
(2)在API11以后的创建方式。NotificationCompat.getNotification()
(3)在API11以后的创建方式。NotificationCompat.build()
在创建Notification当然也可以指定自己的布局文件。比如音乐播放器的Notification。当然创建自定义布局的Notification不在本文的讨论范围内。
我今天要说的是如何在通知中显示若干行内容。我们做常见的通知是这样的。如图1
我今天要实现的样式:
代码如下:
*
private void createNotification(int id,String title,String content)
{ BitmapDrawable drawable =
((BitmapDrawable)(getResources().getDrawable(R.drawable.ic_launcher)));
Bitmap bitmap = drawable.getBitmap(); Intent intent = new
Intent(NNApplication.getInstance(), ClickNotificationReceiver.class);
intent.putExtra(ORDER_ID,orderId);
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(NNApplication.getInstance(), id, intent,
PendingIntent.FLAG_CANCEL_CURRENT); NotificationManager manager =
(NotificationManager)
NNApplication.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = null; if(!CommonUtils.checkIsXiaoMi())
{ NotificationCompat.Builder builder = new
NotificationCompat.Builder(NNApplication.getInstance());
notification = builder.setTicker(“XXXXX”).setContentTitle(“XXXXX”)
.setSmallIcon(R.drawable.ic_launcher).setLargeIcon(bitmap).setWhen(System.currentTimeMillis())
.setStyle(new
NotificationCompat.BigTextStyle().bigText(“采薇采薇,薇亦柔止。曰归曰归,心亦忧止。
忧心烈烈,载饥载渴。我戍未定,靡使归聘。”)
).setContentIntent(pendingIntent).setPriority(Integer.MAX_VALUE).
setWhen(System.currentTimeMillis()).build(); } else {
notification = new Notification(); notification.icon =
R.drawable.ic_launcher; notification.tickerText = “XXX”;
notification.when = System.currentTimeMillis();
notification.contentIntent = pendingIntent;
notification.setLatestEventInfo(NNApplication.getInstance(),”XXX”,content,pendingIntent);
} //用于当点击完通知以后,直接使通知消失。 notification.flags =
Notification.FLAG_AUTO_CANCEL; notification.defaults =
Notification.DEFAULT_SOUND; manager.notify(1, notification); }
*
在上面的生成Notification加了个逻辑判断,即当前的系统是否是小米系统,如果是小米系统则只能按照第一种方法创建Notification。
第一次写博客,写的不好,望海涵。
以下是参考文献:
1.http://blog.csdn.net/loongggdroid/article/details/17616509
2.http://www.2cto.com/kf/201502/374946.html
3.http://stackoverflow.com/questions/14602072/styling-notification-inboxstyle