Android 5.X 新特性详解(五)——Notification
来源:互联网 发布:js判断日期是否合法 编辑:程序博客网 时间:2024/05/16 23:56
Google在Android 5.0 上又进一步改进了通知栏,优化了Notification。当长按Notification的时候,会显示消息来源。Notification会有一个从白色到灰色的动画切换效果,最终显示发出这个Notification的调用者。同时,在Android 5.X 设备上,锁屏状态下我们也可以看见Notification通知了。
基本Notification
/** * 给点击Notification后要执行的操作增进一个Intent,由于这个Intent不是马上执行的,而是由用户触发的, * 所以Android给这样的Intent提供了一个PendingIntent来帮助完成这样的延迟操作。 */ Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com")); // 构造PendingIntent PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); // 创建Notification对象 Notification.Builder builder = new Notification.Builder(this); // 设置Notification的各种属性 builder.setSmallIcon(R.mipmap.ic_launcher); builder.setContentIntent(pendingIntent); builder.setAutoCancel(true); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); builder.setContentTitle("Basic Notifications"); builder.setContentText("I am a basic notification"); builder.setSubText("it is really basic"); // 通过NotificationManager来发出Notification NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //调用notify方法时,需要传进去一个ID。每个Notification都会有一个ID,这个ID就是用来区分不同的APP的Notification的。 notificationManager.notify(NOTIFICATION_ID_BASIC, builder.build());
折叠式Notification
折叠式Notification也是一种自定义视图的Notification,常常用于显示长文本。它拥有两个视图状态,一个是普通状态下的视图状态,另一个是展开状态下的视图状态。
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.sina.com")); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Notification.Builder builder = new Notification.Builder(this); builder.setSmallIcon(R.mipmap.ic_launcher); builder.setContentIntent(pendingIntent); builder.setAutoCancel(true); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); //通过RemoteViews来创建自定义的Notification视图 RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification); contentView.setTextViewText(R.id.textView, "show me when collapsed"); RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.notification_expanded); Notification notification = builder.build(); //将一个视图指定为Notification正常状态下的视图 notification.contentView = contentView; //将一个视图指定为Notification展开时的视图 notification.bigContentView = expandedView; NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.notify(NOTIFICATION_ID_COLLAPSE, notification);
悬挂式Notification
这种被称为Headsup的Notification方式,可以在屏幕上方产生Notification且不会打断用户的操作,能给用户以Notification形式的通知。
Notification.Builder builder = new Notification.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setPriority(Notification.PRIORITY_DEFAULT) .setCategory(Notification.CATEGORY_MESSAGE) .setContentTitle("Headsup Notification") .setContentText("I am a Headsup notification.") .setSubText("Heads-Up Notification on Android 5.0"); Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setClass(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); //通过setFullScreenIntent将一个Notification变成悬挂式Notification builder.setFullScreenIntent(pendingIntent, true); NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.notify(NOTIFICATION_ID_HEADSUP, builder.build());
显示等级的Notification
Android 5.X 将Notification分成了三个等级。
- VISIBILITY_PRIVATE——表明只有当没有锁屏的时候会显示
- VISIBILITY_PUBLIC——标明在任何情况下都会显示
- VISIBILITY_SECRET——表明在pin、password等安全锁和没有锁的情况下才能够显示
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.visibility_radio_group); Notification.Builder builder = new Notification.Builder(this) .setContentTitle("Notification for Visibility Test") //设置Notification显示的位置 .setCategory(Notification.CATEGORY_EMAIL) //设置Notification背景颜色 .setColor(Color.RED); switch (radioGroup.getCheckedRadioButtonId()) { case R.id.radio_button_public: //设置Notification的显示等级 builder.setVisibility(Notification.VISIBILITY_PUBLIC); builder.setContentText("Public"); builder.setSmallIcon(R.mipmap.ic_public); break; case R.id.radio_button_private: builder.setVisibility(Notification.VISIBILITY_PRIVATE); builder.setContentText("Private"); builder.setSmallIcon(R.mipmap.ic_private); break; case R.id.radio_button_secret: builder.setVisibility(Notification.VISIBILITY_SECRET); builder.setContentText("Secret"); builder.setSmallIcon(R.mipmap.ic_secret); break; } NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.notify(NOTIFICATION_ID_VISIBILITY, builder.build());
程序运行效果图如下:
代码地址
0 0
- Android 5.X 新特性详解(五)——Notification
- Android 5.X 新特性详解(五)Toolbar和Notification
- Android 5.X 新特性详解(五)——Toolbar
- Android 5.X 新特性详解(一)——主题、Palette、阴影、着色和裁剪
- Android 5.X 新特性详解(二)——列表与卡片
- Android 5.X 新特性详解(三)——Activity过渡动画
- Android 5.X 新特性详解(四)——Material Design 动画效果
- Android N新特性——Notification快速回复
- 《Android群英传》学习笔记:Android 5.X新特性详解
- Android 5.X 新特性
- Android 5.X新特性
- 《Android群英传》读书笔记(12)第十二章:Android 5.X新特性详解
- Android 5.X 新特性详解(二)列表与卡片
- Android 5.X 新特性详解(三)Activity过渡动画
- Android 5.X 新特性详解(四)Material Design动画效果
- Android群英传知识点回顾——第十二章:Android5.X新特性详解
- Android群英传笔记——第十二章:Android5.X 新特性详解,Material Design UI的新体验
- Android 5.x Support Library 新特性
- 五、继承、派生、复合
- NavigationView使用
- RealGetWindowClass和GetClassName的区别
- 【Android】@+id与@id的区别
- Android的HandlerThread的使用总结
- Android 5.X 新特性详解(五)——Notification
- 关于缓存的处理
- 方法里获取外界变量名和值;
- 单个和多个checkbox选中事件
- 优化UITableViewCell高度计算的那些事
- 高德地图判断地址输入是否正确
- Openfire Connection Manager 配置
- 【转】Io 异常: The Network Adapter could not establish the connection
- mount永久挂载一目录