android 通知条效果
2015-10-26 19:03
369 查看
1.0 官方
在 android 在5.0中加入个通知新样式 Heads-Up.google 介绍界面,需要翻墙
在 Android 5.0的使用的方法的代码:
notification=new NotificationCompat.Builder(MainActivity.this) .setVisibility(Notification.VISIBILITY_PUBLIC) .setSmallIcon(R.drawable.ic_launcher) .setFullScreenIntent(pendingIntent, false) .setContentTitle("这是标题") .setContentText("这是内容") .addAction(R.drawable.ic_launcher, "菜单1", peddingIntent1) .build(); notificationManager.notify(1, notification);
其中
Builder.setFullScreenIntent(pendingIntent, false)是关键;
setUsesChronometer (boolean )设置这个为 true 以后 heads-up会将
action 也显示出来.
2.0 使用库实现类似的方法(向下兼容2.3)
下面介绍一个自己写的,类似 Heads-up 组件的库.github 源码地址
simple heads-up (no expand)
使用方法:
HeadsUpManager manage = HeadsUpManager.getInstant(getApplication()); HeadsUp.Builder builder = new HeadsUp.Builder(MainActivity.this); builder.setContentTitle("提醒").setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS) //要显示通知栏通知,这个一定要设置 .setSmallIcon(R.drawable.icon) //2.3 一定要设置这个参数,否则会报错 .setContentIntent(pendingIntent) .setContentText("你有新的消息"); HeadsUp headsUp = builder.buildHeadUp(); manage.notify(code++, headsUp);
simple heads-up
使用方法:
HeadsUpManager manage = HeadsUpManager.getInstant(getApplication()); HeadsUp.Builder builder = new HeadsUp.Builder(MainActivity.this); builder.setContentTitle("提醒").setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS) //要显示通知栏通知,这个一定要设置 .setSmallIcon(R.drawable.icon) .setContentText("你有新的消息") //2.3 一定要设置这个参数,否则会报错 .setContentIntent(pendingIntent) //设置是否显示 action 按键 .setUsesChronometer(true) .addAction(R.drawable.ic_cloud_queue_black_24dp, "查看", pendingIntent); HeadsUp headsUp = builder.buildHeadUp(); manage.notify(code++, headsUp);
使用自定义布局,支持 button 的点击事件.
使用方法:
final HeadsUpManager manage1 = HeadsUpManager.getInstant(getApplication()); View view=getLayoutInflater().inflate(R.layout.custom_notification, null); view.findViewById(R.id.openSource).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/zzz40500/HeadsUp")); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); MainActivity.this.startActivity(intent); manage1.cancel(); } }); HeadsUp headsUp1 = new HeadsUp.Builder(MainActivity.this) .setContentTitle("标题") //要显示通知栏通知,这个一定要设置 .setSmallIcon(R.drawable.icon) //2.3 一定要设置这个参数,否则会报错 .setContentIntent(pendingIntent) .setContentText("这个是自定义通知") .buildHeadUp(); headsUp1.setCustomView(view); manage1.notify(code++, headsUp1);
heads-up.gif
局限性:
heads-up 没有加入阴影效果:
没有对消息进行优先级排序,现在的排序算法是先进先出.
miui 系统开启悬浮窗才可以正常使用. (miui 真讨厌!!!!)
1.0.1更新内容;
增加了背景阴影部分.
增加了消息横向滑动时候的 alpha 值的改变
增加了常驻消息:调用
HeadsUp.setSticky(true),用
HeadsUpManager.cancel(code)取消.
修改了滑动动画.
向下兼容2.3
1.1.0更新内容;
增加了圆形头像,基于CircleImageView上做的修改
增加在 android 5.0 上使用原生的 headsUp.
增加了方法
setActivateStatusBar(boolean )默认 true ,设置
false 表示只显示 heads-up ,而不会发送到通知栏上.
1.1.0注意:
*使用原生的 headsUp 逻辑是:
系统是大于21,且headsUp 没有使用
setCustomView(View )也没有设置
setIsActivateStatusBar(true)则使用原生的
heads-up
如果要调用了原生的 heads-up,所以你得设置
setFullScreenIntent(pendding,false)pendding不能为空,否则没有效果
相关文章推荐
- android:LocationManager详解(-)
- AES在Android、JAVA端正常加密解密
- 调试Android USB遇到的令人费解的问题
- Android项目能运行,上传svn后再下载却不能运行
- Android添加顶部通知
- [Android开发从零开始].2.模拟器的使用.mp4
- 获取Android设备heapsize
- android studio配置使用
- Android HandlerThread 完全解析
- Android自己定义组件系列【5】——高级实践(1)
- Android-Universal-Image-Loader类库三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- Android图像处理整理
- 【Android成长之路】最常用和最难用的控件——ListView的浅谈(定制ListView 的界面)
- Android 三大图片缓存原理、特性对比
- Android手势密码实现方案
- Android项目布局中android_xxx和android_layout_xxx之间的区别
- android 相册 裁剪 提示失败
- Android检查手机上是否安装了指定的软件(根据包名检测)
- Android 隐藏导航栏
- 使用Android Studio创建Android项目