Android5.x Notification应用解析

本文详细介绍了Android 5.x中三种类型的Notification:普通Notification、折叠式Notification和悬挂式Notification。普通Notification用于基本状态栏提示;折叠式Notification适用于显示长文本和自定义视图;悬挂式Notification则在5.0以上版本提供,无需下拉即可显示,且在一段时间后自动消失。此外,还讨论了Notification的显示等级设置。
摘要由CSDN通过智能技术生成

Notification可以让我们在获得消息的时候,在状态栏,锁屏界面来显示相应的信息,很难想象如果没有Notification,那我们的qq和微信以及其他应用没法主动通知我们,我们就需要时时的看手机来检查是否有新的信息和提醒着实让人烦心,也体现出Notification重要性。这里会介绍三种Notification,分别是普通的Notification,折叠式Notification和悬挂式Notification。

1. 普通Notification

首先创建Builder 对象,用PendingIntent 控制跳转,这里跳转到网页

Notification.Builder builder = new Notification.Builder(this);
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/itachi85/"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);

有了builder 我们就可以给Notification添加各种属性:

 builder.setContentIntent(pendingIntent);
 builder.setSmallIcon(R.drawable.lanucher);
 builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),     R.drawable.lanucher));
 builder.setAutoCancel(true);
 builder.setContentTitle("普通通知");

最后是创建NotificationManager调用notify方法:

  notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  notificationManager.notify(0, builder.build());

来看看效果:
这里写图片描述

2. 折叠式Notification
折叠式Notification是一种自定义视图的Notification,用来显示长文本和一些自定义的布局的场景。它有两种状态,一种是普通状态下的视图(如果不是自定义的话和上面普通通知的视图样式一样),一种是展开状态下的视图。和普通Notification不同的是,我们需要自定义的视图,而这个视图显示的进程和我们创建视图的进程不再一个进程,所以我们需要使用RemoteViews,首先要使用RemoteViews来创建我们的自定义视图:

 //用RemoteViews来创建自定义Notification视图
RemoteViews remoteViews = new Re
评论 31
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值