安卓关于Notification中setLatestEventInfo方法已弃用

今天在更新项目时,把API版本向上更新到25,编译时发现报了一个错误,但是一脸懵逼,后来查了资料才知道在6.0之后setLatestEventInfo方法已被google弃用,为了项目的健全性,无奈只能寻找替代方法。
在6.0之前,使用notification我们一般会这样写

NotificationManager manager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);  

Notification  notification=new Notification(R.drawable.icon_notification,"标题", System.currentTimeMillis());  

notification.setLatestEventInfo(context,"标题",pushInfo,null);  
manager.notify(R.drawable.icon_notification,notification);  

这样完成一个任务栏消息提示原版很正常,但是今天把API版本提升为25之后,编译时这段代码报错,提示setLatestEventInfo方法被弃用,那解决方案其实很简单,我就不过多的文字赘述了,直接贴上代码。

6.0之后如下编写

NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);  
//         
        Notification notification = new Notification.Builder(this)  
                .setContentTitle("标题")  
                .setContentText("内容")  
                .setSmallIcon(R.mipmap.ic_launcher)  
//                .setLargeIcon(mBitmap)  
                .build();  

       nm.notify(1,notification);  

猜你喜欢

转载自blog.csdn.net/xk7298/article/details/81589243