绪论
Android O已经发布了一段时间,虽然搭载Android O的手机还没有上市,实际软件还不用考虑O的版本兼容性问题,但是O对Api的一些修改仍然会导致未来App软件兼容性的问题,今天我就和大家谈一谈我遇到的关于传统Notification不能正常使用的问题,首先请看下述的两个warning:
W/Notification: Use of stream types is deprecated for operations other than volume control
W/Notification: See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case
大概意思就是说,你的方法过时了!如果在Android O的模拟器中使用传统的Notification,伴随这两个warning的结果是:
显然,你所设置的Notification是不能正常出现的,下面我就来分析下具体情况。
传统的Notification的使用
Notification的使用主要用到两个东西:NotificationManager、Notification。
其中,Notification是具体我们需要显示的通知,它的api可以经过一系列的属性修改来达到我们想要的内容。而NotificationManager我们可以理解为一个管理者,它能够控制对象们的更新等等操作,当然,这里对象指的是我们的Notification,具体的操作看代码,不解释:
NotificationManager mNotificationManager =(NotificationManager) getSystemService(NOTIFICATION_SERVICE);//得到一个manager对象
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentTitle("您有一条新通知")
.setContentText("这是一条逗你玩的消息")
.build();//以上采用Builder模式,获得notification
mNotificationManager.notify(1, notification);//将其显示出来,这个1主要是指notification的ID
使用以上的代码是能够正常显示一条通知的,但是一般为了保证用户体验