前言
Android N引入了一些新的API,其中增加了快速回复,捆绑通知,自定义视图和消息样式,这篇文章主要给大家讲一下快速回复功能是如何实现的。
首先我们利用Button的点击事件去触发添加一个Notification,代码如下:
MainActivity.Java
|
|
上面的代码写好之后,我们点击Button,通知栏就会弹出一个Android N风格的Notification,如下图:
点击Notification中的快速回复按钮,就会弹出一个编辑框,让用户输入要回复的内容,如下图:
到这里,我们的Android N特性的Notification就添加完成了,下面再说一说如何获取快速回复中的内容。上面我们设置了一个pendingIntent,这个pendingIntent的作用就是当用户点击发送的时候,会发送一个广播出去,GetMessageReceiver就会接受到这个广播,我们可以在这个广播中获取用户输入的回复信息,代码如下:
GetMessageReceiver.java
|
|
上述代码是GetMessageReceiver这个广播接收器中的一些操作,并且模拟了发送短信的过程
注意:BroadcastReceiver的生命周期只有十秒钟左右,如果耗时操作时间过长,就会引起ANR,这里是为了简便代码,所以才在BroadcastReceiver中添加了模拟发送信息这个耗时操作,大家以后用到的时候,可以在BroadcastReceiver中开启一个Service,然后在这个Service中进行一些耗时的操作!!!!!!!!!
好了,到这里为止,整个Android N Notification快速回复的新特性内容已经讲完了,如果有什么不对的地方,欢迎大家指正,谢谢!