今天看《第一行代码》第八章,里面介绍到通知的使用,看了基本的使用方法后,我就迫不及待的打起了代码。
布局就是简单的按钮,在MainActivity里的主要代码里出现了一点小小的问题
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification=new Notification(R.mipmap.ic_launcher,"This is ticker text",System.currentTimeMillis());
notification.setLatestEventInfo(this,"This is content title","This is content text",null);
manager.notify(1, notification);
break;
发现setLatestEventInfo显示为红色,去网上一查,发现是已经弃用的方法,这里推荐一篇文章写关于notification的,还不错
http://blog.csdn.net/loongggdroid/article/details/17616509
API16以后,使用的是Builder替代,将代码改成
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("您有新的短消息,请注意查收")
.setContentInfo("contentInfo")
.setContentTitle("Notification Title")
.setContentText("This is the notification message")
.setContentIntent(pi)
.setNumber(1)
.build();
而API11以后,可以用getNotification替代Builder,不过在API16以后也不推荐