Android Notification.setLatestEventInfo弃用和替代方法

相关文章  通知的各种类型

Android6.0后setLatestEventInfo和new Notification()方法弃用,首先看一下setLatestEventInfo方法的使用:

Notification notification = new Notification(drawable,tickerText,System.currentTimeMillis()); //此处定义了一个Notification 
其中第一个参数代表图标第二个参数代表提示的内容,第三个参数是指要显示的时间,一般是当即显示,故填入系统当前时间。
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, ActivityMain.class), 0);//该语句的作用是定义了一个不是当即显示的activity,只有当用户拉下notify显示列表,并且单击对应的项的时候,才会触发系统跳转到该activity.

notification.setLatestEventInfo(this, title, content, contentIntent);//在此处设置在notity列表里的该notifycation得显示情况。

替代方法:

notification = new Notification.Builder(context)
      .setChannelId(String.valueOf(Notification_ID))//该句适配android 8.0 版本
      .setSmallIcon(notifyIcon)
      .setContentTitle(notifyTitle)
      .setTicker(text)
      .setContentText(text)
      .setWhen(when)
      .setContentIntent(pi).setNumber(0)
      .getNotification();
notification.flags |= Notification.FLAG_AUTO_CANCEL;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值