Android notification的基本使用

今天看《第一行代码》第八章,里面介绍到通知的使用,看了基本的使用方法后,我就迫不及待的打起了代码。

布局就是简单的按钮,在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以后也不推荐

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值