Android 使用Notification(通知)

        今天在API Level 23上使用Notification的时候遇到了点问题,于是就去查了一下. 发现在不同的版本下,使用的方式存在着一些差异.

1. API Level < 11

        在API Level < 11的版本下, 通知的实现方法是使用LatestEventInfo()方法. 在很多网上的资料和书籍上都有例子.

        NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.ic_launcher, "tickerText", System.currentTimeMillis());
        notification.setLatestEventInfo(this, "Title", "Text", null);
        manager.notify(1, notification);
2. 11 < API Level < 16

        此时就要在Notification.Builder里设置通知的参数了.

        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(NotificationActivity.this);
        builder.setAutoCancel(true)
         .setSmallIcon(R.drawable.ic_launcher)
         .setContentTitle("Title")
         .setTicker("Ticker")
         .setContentText("Text")
         .setWhen(System.currentTimeMillis())
         .setContentIntent(null);
        Notification notification = builder.getNotification();
3. 16 < API Level

        此时,推荐使用build()方法来设置通知的参数.不过,Notification.Builder也能用.

Notification notification = new Notification.Builder(NotificationActivity.this)
                            .setAutoCancel(true)
                            .setWhen(System.currentTimeMillis())
                            .setSmallIcon(R.drawable.zaza)
                            .setContentTitle("Title_one")
                            .setContentText("Text_one")
                            .build();
  

        获取版本的方法 : Build.VERSION.SDK_INT.

if(Build.VERSION.SDK_INT > 16){
        ......
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值