Android N新特性——Notification快速回复
来源:互联网 发布:淘宝开店怎么推广宣传 编辑:程序博客网 时间:2024/05/20 15:10
Android N引入了一些新的API,其中增加了快速回复,捆绑通知,自定义视图和消息样式,这篇文章主要给大家讲一下快速回复功能是如何实现的。
首先我们利用Button的点击事件去触发添加一个Notification,代码如下:
MainActivity.java
public static final String KEY_TEXT_REPLY = "key_text_reply";//获取快速回复内容的keypublic static final int NOTIFICATION_ID = 100;......private void addNotification(){ String replyLabel = getResources().getString(R.string.reply_label); //创建一个远程输入(既:通知栏的快捷回复) RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY) .setLabel(replyLabel) .build(); //点击快速回复中发送按钮的时候,会发送一个广播给GetMessageReceiver Intent intent = new Intent(MainActivity.this,GetMessageReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,intent, PendingIntent.FLAG_ONE_SHOT); //创建快速回复的动作,并添加remoteInut Notification.Action replyAction = new Notification.Action.Builder( R.drawable.ic_chat_blue_24dp, getString(R.string.label), pendingIntent) .addRemoteInput(remoteInput) .build(); //创建一个Notification,并设置title,content,icon等内容 Notification newMessageNotification = new Notification.Builder(this) .setSmallIcon(R.drawable.ic_account_circle_white_24dp) .setContentTitle(getString(R.string.title)) .setContentText(getString(R.string.content)) .addAction(replyAction) .build(); //发出通知 NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(NOTIFICATION_ID, newMessageNotification); }
上面的代码写好之后,我们点击Button,通知栏就会弹出一个Android N风格的Notification,如下图:
点击Notification中的快速回复按钮,就会弹出一个编辑框,让用户输入要回复的内容,如下图:
到这里,我们的Android N特性的Notification就添加完成了,下面再说一说如何获取快速回复中的内容。上面我们设置了一个pendingIntent,这个pendingIntent的作用就是当用户点击发送的时候,会发送一个广播出去,GetMessageReceiver就会接受到这个广播,我们可以在这个广播中获取用户输入的回复信息,代码如下:
GetMessageReceiver.java
@Override public void onReceive(Context context, final Intent intent) { this.context = context; //模拟发送消息 new Thread(new Runnable() { @Override public void run() { try { String replyContent = getMessageText(intent).toString(); Log.i("GetMessageReceiver",replyContent); Thread.sleep(3000);//设置三秒钟之后更新Notification的状态 updateNotification(); Thread.sleep(3000);//三秒钟后移除Notification cancalNotification(); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } //获取快捷回复中用户输入的字符串 private CharSequence getMessageText(Intent intent) { Bundle remoteInput = RemoteInput.getResultsFromIntent(intent); if (remoteInput != null) { **//通过KEY_TEXT_REPLY来获取输入的内容** return remoteInput.getCharSequence(MainActivity.KEY_TEXT_REPLY); } return null; } //消息发送状态的Notification private void updateNotification(){ Notification repliedNotification = new Notification.Builder(context) .setSmallIcon(R.drawable.ic_account_circle_white_24dp) .setContentText("消息发送成功!") .build(); //利用NOTIFICATION_ID同一个ID,更新Notification的内容 notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); notificationManager.notify(MainActivity.NOTIFICATION_ID, repliedNotification); } //移除Notification private void cancalNotification(){ notificationManager.cancel(MainActivity.NOTIFICATION_ID); }
上述代码是GetMessageReceiver这个广播接收器中的一些操作,并且模拟了发送短信的过程
注意:BroadcastReceiver的生命周期只有十秒钟左右,如果耗时操作时间过长,就会引起ANR,这里是为了简便代码,所以才在BroadcastReceiver中添加了模拟发送信息这个耗时操作,大家以后用到的时候,可以在BroadcastReceiver中开启一个Service,然后在这个Service中进行一些耗时的操作!!!!!!!!!
Demo地址:https://github.com/hurui1990/AndroidNDemo
好了,到这里为止,整个Android N Notification快速回复的新特性内容已经讲完了,如果有什么不对的地方,欢迎大家指正,谢谢!
- Android N新特性——Notification快速回复
- Android N新特性:direct reply notification
- android N 新特性
- Android N新特性
- Android N 新特性
- Android N 新特性
- Android N 新特性
- Android 5.X 新特性详解(五)——Notification
- Android N 的新特性
- Android N WiFi 新特性
- html5 notification新特性
- android N 的新特性 android 7.0的新特性
- Android N(SDK7.0)新特性
- Android N新特性--多窗口支持
- Android N 7.0的新特性
- 简单解析 Android N 新特性
- Notification的一些新特性
- Android Notification Sound 特性
- bzoj 2741 【FOTILE模拟赛】L 可持久化字典树+分块
- 字符串拼接及拖拽(js方法扩展code)
- App导航 - 具有android或IOS的特性设计
- bzoj 3166 [Heoi2013]Alo 可持久化字典树
- 【PAT】(乙级)1009. 说反话 (20)
- Android N新特性——Notification快速回复
- poj2676 Sudoku 数独
- bzoj 3261最大异或和 可持久化字典树
- json对象和java对象的相互转换方法(json-lib、jackson、fastjson、gson)
- 软件测试基础(三)---黑盒测试之边界值法
- javaScript 跨域总结与解决办法
- 洛谷 P1967 Vijos P1843 CODE[VS] P3287 [NOIP2013 D1T3] 货车运输
- SurfaceView的看法
- TCP三次握手和四次挥手