Android N 通知栏和快捷通知栏带来的改变

Android N 通知栏和快捷通知栏带来的改变

Android N 引入了一些新的API,允许应用发布具有高度可见性和交互性的通知。

Android N 扩展了现有 RemoteInput 通知 API,以支持手持式设备上的内联回复。 此功能允许用户从通知栏快速进行回复,无需访问应用。

此外,Android N 还允许捆绑类似的通知并将它们显示为一则通知。 为了实现此功能,Android N 使用现有的 NotificationCompat.Builder.setGroup() 方法。用户可以从通知栏展开各通知,并分别对每则通知进行回复和清除等操作。

最后,Android N 还添加了一些新 API,允许您在应用的自定义通知视图中使用系统装饰元素。 这些 API 可帮助确保通知视图与标准模板的展示效果相一致。

本文重点介绍您在应用中使用新通知功能时应加以考虑的一些重要变更。

直接回复

利用 Android N 中的直接回复功能,用户可以直接在通知界面内快速回复短信或更新任务列表。 在手持式设备上,可通过通知中另外附加的按钮进行内联回复操作。 当用户通过键盘回复时,系统会将文本回复附加到您为通知操作指定的 Intent,并将 Intent 发送到手持式设备应用。需要直接回复。

要创建支持直接回复的通知操作:

创建一个可添加到通知操作的 RemoteInput.Builder 实例。 该类的构造函数接受系统用作文本输入密钥的字符串。 之后,手持式设备应用使用该密钥检索输入的文本。

 //远程的输入控件构造一个

   RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY).setLabel("回复点什么呢?").build();

使用 addRemoteInput() 向操作附加 RemoteInput 对象。

    // Create the reply action and add the remote input.
Intent intent = new Intent();
intent.putExtra("id", id);
intent.setClass(MainActivity.this, MainActivity.class);
PendingIntent replyPendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Notification.Action action = new Notification.Action.Builder(R.mipmap.ic_launcher, "点击这里回复东西", replyPendingIntent)
.addRemoteInput(remoteInput).build();

对通知应用操作并发出通知。

 // Build the notification and add the action.
final Notification newMessageNotification = new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("通知栏标题")
.setContentText("通知栏内容").setPriority(Notification.PRIORITY_HIGH).setDefaults(Notification.DEFAULT_VIBRATE).addAction(action).build();
// Issue the notification.
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(id++, newMessageNotification);

然后用户就可以在通知栏看到我们发出的通知并且进行回复了。

得到用户输入数据

从pendingintent换起来的组件我们可以通过intent这样拿到我们的数据。然后通过相同的id发送通知更新我们的回复。

Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
String res = null;
if (remoteInput != null) {
res = (String) remoteInput.getCharSequence(KEY_TEXT_REPLY);
}
Notification repliedNotification = new Notification.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentText(res + "回复成功").build();
// Issue the new notification.使用这个相同的ID冲掉以前的notifacation
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
int id = intent.getIntExtra("id", 1);
Log.d("EDSHENG", "onNewIntent: " + id);
notificationManager.notify(id, repliedNotification);

发送分组的通知

分组通知需要主意的有两点一个是要构造一个分组的信息,然后对子通知进行分组。

注意:如果没有分组普通的通知栏在N上超过4个系统就会默认的分到一个组里面。

 //远程的输入控件构造一个
RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY).setLabel("group回复点什么呢?").build();

// Create the reply action and add the remote input.
Intent intent = new Intent();
intent.putExtra("id", id);
intent.setClass(MainActivity.this, MainActivity.class);
PendingIntent replyPendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Notification.Action action = new Notification.Action.Builder(R.mipmap.ic_launcher, "group点击这里回复东西", replyPendingIntent)
.addRemoteInput(remoteInput).build();

// Build the notification and add the action.

final Notification group = new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("group通知栏标题")
.setGroup("分组信息").setAutoCancel(true).setStyle(new Notification.InboxStyle()).setGroupSummary(true)
.setContentText("group通知栏内容").addAction(action).build();
Notification newMessageNotification = new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("group子通知栏标题")
.setGroup("分组信息").setAutoCancel(true).setStyle(new Notification.InboxStyle())
.setContentText("group子通知栏内容").addAction(action).build();
// Issue the notification.
 NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(id++, newMessageNotification);
notificationManager.notify(1000, group);

通过构造一个分组调用的setGroupSummary方法分为一个组,然后第二个通知栏调用setGroup被分到这个组里面,如果下面还有其他的通知过来也调用这个setGroup为同一组就可以在这个组里面了。

消息传递样式

ndroid N 引入了一项新的 API 来自定义通知样式。 使用 MessageStyle 类,您可以更改在通知中显示的多个标签,包括会话标题、其他消息和通知的内容视图。常用在对话当中。

以下代码段演示了如何使用 MessageStyle 类来自定义通知样式。

Notification notification = new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher)
.setStyle(new Notification.MessagingStyle("Me")
.setConversationTitle("Team lunch")
.addMessage("Hi", System.currentTimeMillis(), null) // Pass in null for user.
.addMessage("What‘s up?", System.currentTimeMillis(), "Coworker")
.addMessage("Not much", System.currentTimeMillis(), null)
.addMessage("How about lunch?", System.currentTimeMillis(), "Coworker")).build();
// Issue the notification.
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(id++, notification);

快捷通知栏

Android N上还加入了一个名字叫TileService的类,这个类主要是针对快捷通知栏的扩展,让开发者可以自定义下拉通知栏的快捷设置图标。

我们可以把我们应用内的一些快捷入口放置在这里。那么我们就来看一下如何实现我们的这个快捷通知栏吧。

我们继承一个TileService然后实现它的onclick方法。我们在这里是启动我们的activity然后在收起我们的下拉通知栏。但是需要注意的是在我们的androidMainifest里面配置。一个是收起和展开通知栏的权限

这个就是就是service的声明。

   <service
android:name=".TitleService"
android:label="免费wifi"
android:icon="@drawable/wifi_panel_signal_good"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
</service>

然后这里就是我们的实现。

public class TitleService extends TileService {
public TitleService() {
}

@Override
public void onTileAdded() {
super.onTileAdded();
}

@Override
public void onTileRemoved() {
super.onTileRemoved();
}

@Override
public void onClick() {
super.onClick();
Intent intent = new Intent();
intent.setClass(getApplicationContext(),MainActivity.class);
//intent.setComponent(new ComponentName("com.tencent.mtt", "com.tencent.mtt.external.wifi.WifiLaunchActivity"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 getBaseContext().startActivity(intent);
collapseStatusBar(getBaseContext());

}

public static void collapseStatusBar(Context context)
{
try
{
Object statusBarManager = context.getSystemService("statusbar");
Method collapse;

if (Build.VERSION.SDK_INT <= 16)
{
collapse = statusBarManager.getClass().getMethod("collapse");
}
else
{
collapse = statusBarManager.getClass().getMethod("collapsePanels");
}
collapse.invoke(statusBarManager);
}
catch (Exception localException)
{
localException.printStackTrace();
}
}
}

如果想更多的了解这个类和方法,可以访问一下这一篇博客:https://medium.com/@KieronQuinn/quick-settings-in-android-n-ea8ad8ce2eea#.aevgyn5o6

Demo的下载地址:http://download.csdn.net/detail/shengbo1992/9573890

时间: 2024-07-30 18:25:16

Android N 通知栏和快捷通知栏带来的改变的相关文章

Android LayoutInflater深度解析 给你带来全新的认识

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38171465 , 本文出自:http://blog.csdn.net/lmj623565791/article/details/38171465 1. 题外话 相信大家对LayoutInflate都不陌生,特别在ListView的Adapter的getView方法中基本都会出现,使用inflate方法去加载一个布局,用于ListView的每个Item的布局.Inflate有三个

android selector(如对TextView点击样式改变)

selector 1.selector 从单词的意思来说:选择者,选择器,就是对你的目标的控制. 从API来说: A controller for the selection of SelectableChannel objects. Selectable channels can be registered with a selector and get a SelectionKey that represents the registration. The keys are also add

解决Android中,禁止ScrollView内的控件改变之后自动滚动

问题: 最近在写一个程序界面,有一个scrollVIew,其中有一段内容是需要在线加载的. 当内容加载完成后,ScrollView中内容的长度会发生改变,这时ScrollView会自动下滚,如下图所示: 滚动的那一下体验特别不好,所以要防止这种情况.即不论Scrollview中内容如何,都要保持在最上. 解决办法: 先简单写一下我的xml文件的结构: [html] view plaincopy <ScrollView android:id="@+id/scrollView1" a

互联网民营银行带来的改变

随着全国第一批民营银行运营一年多各自推出自己的产品,我们可以从民营银行的产品来观察它们所带来的改变,本文试图以微众银行和网商银行这两家典型的互联网民营银行为列来做分析. 互联网银行还是逃不出商业银行的三大基本业务----存贷汇,只是它们更加以客户为中心,更加灵活,更加便利,试图服务更广泛的人群. 存款业务: 互联网银行基本没有存款业务,如微众和网商都没有网点没法揽存,但各自都推出理财产品来代替传统的存款产品,如网商银行的定活宝.余利宝.随意存:微众的余额理财,同时还有代销的理财产品.随意存产品比

部署VDI虚拟桌面带来的改变

部署VDI带来的改变 部署虚拟桌面基础设施之后,员工和IT管理员都会遇到很多方面的变化.不只是员工需要适应新的环境,管理员也需要解决新工具的整体转换问题以及熟悉如何管理虚拟桌面的使用. 部署VDI的一个主要原因就是用户体验.将桌面按照需求交付给用户,而不会受到用户所在位置和使用设备的限制.不论通过View Client.Citrix Receiver.微软远程桌面服务还是其他方式,VDI都可以在恰当的时间将恰当的桌面交付给恰当的用户.事实上,对于用户和IT部门来说,一个积极的主要变化是:更少的停

重点来了!人工智能2018最热产业将为我们的生活带来哪些改变?

今年人工智能产业哪些方向最受关注? 当下,人工智能产业出现九大发展热点领域,分别是芯片.自然语言处理.语音识别.机器学习应用.计算机视觉与图像.技术平台.智能无人机.智能机器人.无人驾驶.据新华社北京3月(记者周文林)报道,最新数据显示,今年人工智能产业无人驾驶.计算机视觉最为热门,同时机器人.智慧医疗极具潜力. 根据知名研究机构赛迪顾问近日发布的<中国人工智能产业投融资白皮书2018>,当前人工智能产业发展较快.随着各地人工智能建设逐步启动,预计到2020年我国人工智能核心产业规模将超过16

5G时代的到来,会给生活带来什么改变?

本文首发于:风云社区. 据百度百科介绍,5G网络是第五代移动通信网络,其峰值理论传输速度可达每秒数十Gb,比4G网络的传输速度快数百倍.举例来说,整部超高画质电影可在1秒之内下载完成. 5G网络已成功在28千兆赫(GHz)波段下达到了1Gbps,相比之下,当前的第四代长期演进(4G LTE)服务的传输速率仅为75Mbps. 5G网络已在海南.云南等地开展试商用,在不久的将来,将会普遍全国. 随着5G技术的诞生,用智能终端分享3D电影.游戏以及超高画质(UHD)节目的时代正向我们走来. 5G支持可

5G能带来什么改变-从鸿蒙OS说起

背景 从5G投票事件开始,开始关注5G.许多文章都说到5G的特点有速度快.时延低,其中,时延低是最重要的特点.然而,时延低能给社会带来什么改变呢? 2G是短信的时代,3G促成了语音视频,4G促成了短视频.直播,那么5G能促成什么?有的说是自动驾驶. 带着这个疑问,生活依然继续着. 一.关于华为云电脑 之前媳妇换手机,买了华为mate10 pro,我有空就会拿过来玩玩,偶尔了解一下相关的新闻.有一天,我看到华为推出了“华为云电脑”应用,媳妇的手机刚好能试用.我于是就试用了一番,没有什么卡顿,电脑配

android 使用信鸽推送通知栏不显示推送的通知?

跟往常一样使用信鸽推送,不过这次却怎么也没反应.经过查看log发现确实是收到了推送过来的消息了,其中有这么一行: W/dalvikvm(23255): VFY: unable to resolve virtual method 1345: Landroid/support/v4/app/NotificationCompat$Builder;.build ()Landroid/app/Notification; 貌似c调用android的构造通知的那些函数调用不到,导致通知不能正常显示.看来问题出